Nested forms in rails 3.1

孤街浪徒 提交于 2019-12-10 18:46:17

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/7489431/nested-forms-in-rails-3-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!