rails 视图中的类变量? [英] Class variables in rails views?
本文介绍了rails 视图中的类变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在视图中引用 ruby 类变量?
Is it possible to refer to ruby class variables in a view?
推荐答案
更常见的方法是将类变量包装在一个辅助方法中:
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 -%>."
这篇关于rails 视图中的类变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文