Question

I'm new in ruby and rails. And now a little confused about the following problem.

I have a form where the user can add/edit entries via a form. The form contains input fields, labels, checkboxes and comboboxes (select).

If the user wants to edit an object, the stored data is loaded correctly, but if the data of the checkbox should be added, e.g. item is selected or not, I get this error:

ActionView::Template::Error (undefined method 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'

The following is a small example of my _form_input,html.erb, but it also causes the same error.

    <%= 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 %>

I develop and test this on two systems. My testing system the following versions:

Ruby version 1.9.3 (i686-linux)
Rails version 3.2.13

The production system:

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

I've read the ruby documentation of enumerable v1.8.7 and v1.9.3, but can not found the mistake. If I remove the f.submit (line 14) it works.

I know the error is caused by book.source_formats.any? { |f| f.id == 4 } and it only occurs in v1.8.7. Any ideas?

Was it helpful?

Solution

This line

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

overrides f reference. Change it to f.x.:

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

This answers the question why?: Variable Scope in Blocks

Variable scope for blocks was introduced in 1.9

OTHER TIPS

<%= form_for :book, :url => { :action => action }, :method => method do |f| %>

here f is reference variable for your form block, it gets overridden here in the following line

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

change local block variable here for book source to something like say bs

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

It will solve the problem. Forker gave you a good link about variable scope in blocks for 1.9.x ruby.

Variable scope in blocks for ruby 1.9+ has been discussed here @ Variable Scope in Blocks and same name variable f reference is causing this issue. Change variable name for book source format like the following book.source_formats.any? {|sf| sf.id == 4}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top