我正在尝试从项目的haml视图访问帮助程序变量。我在定义了帮助器rails-root/app/helpers/table_field_helper.rb
:
module TableFieldHelper
def table_display_fields
MORE_COMPLICATED_TABLE_DISPLAY_FIELDS #some array defined elsewhere in the file
end
...
end
并尝试table_display_fields
在index.html.haml中进行迭代:
...
%tr
%th
- table_display_fields.each do |field|
%th= field
...
我收到一个错误: undefined local variable or method 'table_display_fields'
是什么导致此错误?为什么我无法访问TableFieldHelper中的定义?我是Ruby / Rails的新手,但我假设所有帮助程序都隐式包含在视图/布局文件中,并且我读过的书没有建议我做任何额外的工作才能使它们协同工作。谢谢!
如果帮助程序类名称与控制器名称不同,那么您将必须手动要求该帮助程序类。http://api.rubyonrails.org/classes/ActionController/Helpers.html您可以将helper类的名称更改为与您在其中使用的控制器相同的名称,或者可以将其包含在控制器中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句