views:

211

answers:

2

Is it possible to refer to ruby class variables in a view?

+2  A: 

Unfortunately class (@@variables) are not copied into the view. You may still be able to get at them via:

controller.instance_eval{@@variable}

or

@controller.instance_eval{@@variable}

or something else less gross.

With Rails, 99.9 out of 100 people should never be doing this, or at least I can't think of a good reason.

cwninja
while this isn't what I plan to use, it does answer my original question
Tom Bennett
+1  A: 

The more common approach is to wrap the class variable in a helper method:

# in /app/controllers/foo_controller.rb:
class FooController < ApplicationController
  @@bar = 'baz'

  def my_action
  end

  helper_method :bar

  def bar
    @@bar
  end
end

# in /app/views/foo/my_action.html.erb:
It might be a class variable, or it might not, but bar is "<%= bar -%>."
James A. Rosen
pretty much the obvious solution, don't know why I didn't opt for this in the first place
Tom Bennett