Rails Undefined Helper方法调用同一助手

时间:2014-11-13 17:55:28

标签: ruby-on-rails ruby ruby-on-rails-3 metaprogramming

所以,我有一个奇怪的问题。我在rails 3.2.9中有一个Helper方法,当试图在同一个模块中调用另一个方法时,它一直给我一个找不到错误的方法。我已经在rails控制台中对它进行了测试,一切都按预期工作。这是我的代码:

助手模块:

module SomeHelper
  def options_for(field, options={})    SomeHelper.send(field).merge options end

  def seller_f_name_options()           {placeholder: 'First Name'} end
  def seller_l_name_options()           {placeholder: 'Last Name'} end
  def seller_email_options()            {placeholder: 'Email'} end
end

控制器视图

<%= f.text_field :seller_l_name, options_for(:seller_l_name_options, {placeholder: 'AltFNText'}) %>

Rails错误:

    NoMethodError in Landing_pages#index

Showing /var/www/application/app/views/landing_pages/_form_lp2.html.erb where line #24 raised:

undefined method `seller_l_name_options' for SomeHelper:Module
Extracted source (around line #24):

21:       </div>
22:       <div style="float:right; width:50%;">
23:         <%= f.label "Last Name", :style=>"margin-left: 8px;" %><br>
24:         <%= f.text_field :seller_l_name, options_for(:seller_l_name_options, {placeholder: 'None'}) %>
25:       </div>
26:     </div>
27: 
Trace of template inclusion: app/views/landing_pages/template2.html.erb

Rails.root: /var/www/application

Application Trace | Framework Trace | Full Trace
app/helpers/some_helper.rb:2:in `options_for'
app/views/landing_pages/_form_lp2.html.erb:24:in `block in _app_views_landing_pages__form_lp__html_erb__4594666433786154550_70236178320020'
app/views/landing_pages/_form_lp2.html.erb:8:in `_app_views_landing_pages__form_lp__html_erb__4594666433786154550_70236178320020'
app/views/landing_pages/template2.html.erb:69:in `_app_views_landing_pages_template__html_erb__187408593000100027_35017480'
app/controllers/landing_pages_controller.rb:29:in `index'
config/initializers/quiet_assets.rb:7:in `call_with_quiet_assets'

非常感谢有关此问题的任何帮助!

1 个答案:

答案 0 :(得分:1)

没有必要通过模块调用它,我的意思是SomeHelper.send(field)。所有这些方法都是实例方法,因此仅使用send(field)就足够了。

这个问题更为详尽here