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
2009-10-15 23:19:44
while this isn't what I plan to use, it does answer my original question
Tom Bennett
2009-10-16 18:11:18
+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
2009-10-16 01:34:55
pretty much the obvious solution, don't know why I didn't opt for this in the first place
Tom Bennett
2009-10-16 18:08:27