【问题标题】:Ruby error any? cause an ActionView::Template::Error - undefined methodRuby 有错误吗?导致 ActionView::Template::Error - 未定义的方法
【发布时间】:2014-02-13 13:13:48
【问题描述】:

我是 ruby​​ 和 rails 的新手。现在对以下问题有点困惑。

我有一个表单,用户可以通过表单添加/编辑条目。该表单包含输入字段、标签、复选框和组合框(选择)。

如果用户想要编辑一个对象,存储的数据会被正确加载,但是如果要添加复选框的数据,例如项目是否被选中,我得到这个错误:

ActionView::Template::Error(未定义方法submit' for #<Format id: 4, format_name: "RTF">):
11: <p><%= f.label :isbn13 %><br /><%= f.text_field :isbn13%></p>
12: <%end%>
13: <%= debug @book.source_formats.any? { |f| f.id == 4 } %>
14: <p><%= f.submit msg %></p>
15: <% end %>
16: <% content_for :header_tags do %>
17: <%= javascript_include_tag "book_form", :plugin => 'books' %>
/usr/lib/ruby/vendor_ruby/active_model/attribute_methods.rb:407:in
method_missing'
/usr/lib/ruby/vendor_ruby/active_record/attribute_methods.rb:149:in `method_missing'

下面是我的_form_input,html.erb的一个小例子,但也会导致同样的错误。

    <%= form_for :book, :url => { :action => action }, :method => method do |f| %>
    <%= field_set_tag(l(:fieldset_book_info)) do%>
            <%= f.hidden_field :id , :value => @book.id %>
            <p><%= label(:create_ticket, "checked", l(:label_create_ticket)) %><%= check_box :create_ticket, "checked", :checked => "checked", :disabled => Setting.plugin_books['create_ticket'], :checked_value => true, :unchecked_value => false  %></p>
            <p><%= f.label :title %><br /><%= f.text_field :title %></p>
            <p><%= f.label :isbn10 %><br /><%= f.text_field :isbn10 %></p>
            <p><%= f.label :isbn13 %><br /><%= f.text_field :isbn13%></p>
    <%end%>
          <%= debug @book.source_formats.any? { |f|  f.id == 4 } %>     
         <p><%= f.submit msg  %></p>
<% end %>

我在两个系统上开发和测试它。我的测试系统有以下版本:

Ruby 版本 1.9.3 (i686-linux)
Rails 3.2.13 版

生产系统:

Ruby 版本 1.8.7-p358 (2012-02-08) [x86_64-linux]
Rails 3.2.13 版

我已经阅读了 enumerable v1.8.7 和 v1.9.3 的 ruby​​ 文档,但没有发现错误。如果我删除 f.submit (line 14) 它可以工作。

我知道这个错误是由book.source_formats.any? { |f| f.id == 4 } 引起的,它只发生在 v1.8.7 中。有什么想法吗?

【问题讨论】:

  • 您在&lt;%= f.submit msg %&gt; 之前有&lt;%end%&gt; 显示完整列表您的_form_input,html.erb

标签: ruby erb


【解决方案1】:

这一行

<%= debug @book.source_formats.any? { |f|  f.id == 4 } %>

覆盖f 引用。将其更改为 f.x.:

<%= debug @book.source_formats.any? { |sf|  sf.id == 4 } %>

这回答了为什么?:Variable Scope in Blocks

块的可变范围是在 1.9 中引入的

【讨论】:

  • 感谢很多叉子。这是一个愚蠢的初学者错误!
【解决方案2】:
<%= form_for :book, :url => { :action => action }, :method => method do |f| %>

这里 f 是表单块的引用变量,它在下面的行中被覆盖

<%= debug @book.source_formats.any? { |f|  f.id == 4 } %>

在此处将书源的本地块变量更改为 bs 之类的东西

<%= debug @book.source_formats.any? { |bs|  bs.id == 4 } %>

它会解决问题。 Forker 为您提供了一个关于 1.9.x ruby​​ 块中变量范围的良好链接。

【讨论】:

    【解决方案3】:

    这里已经讨论了 ruby​​ 1.9+ 的块中的变量范围@Variable Scope in Blocks,并且同名变量 f 引用导致了这个问题。更改书籍源格式的变量名称,如下book.source_formats.any? {|sf| sf.id == 4}

    【讨论】:

      猜你喜欢
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 2013-04-10
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多