TsukinoMai
TsukinoMai

Reputation: 417

Nested forms in rails 3.1

I have a problem with nested forms: rails 3.1 doesn`t render fields_for blocks when it should (when editing existing record for example). Since I`m not confident enough in my english, I`ve made a small example app:

New Action:

def new
  @manga = Manga.new
  3.times {@manga.volumes.build}
end

Form Code:

<%= form_for @manga do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <% f.fields_for :volumes do |builder| %>  
  <p>  
    <%= builder.label :cover_link, "Link to cover" %><br />  
    <%= builder.text_field :cover_link %>  
  </p>  
  <% end %>  
  <p><%= f.submit %></p>
<% end %>

In Rails 3.0 the resulting form looks just like it should. (OK, so I can't post images. So I have to put them as links instead.) But in rails 3.1 the result is different.

I probably should also note, that I`ve no problems with saving and whatever else most similar questions ask. Everything is saved perfectly, when fields are added with JS from railscast 197. The main problem here is that everything saved is impossible to edit.

Upvotes: 3

Views: 1283

Answers (1)

TsukinoMai
TsukinoMai

Reputation: 417

And after lurking around I found out, that my question was not that different from the others.

Deprecated way to call fields_for was the fault.

The fact that data was being saved (and fields were being added) through JS, kind of mislead me. Solution was quite simple:

not `<% fields_for %>`, but `<%= fields_for %>`

Upvotes: 4

Related Questions