I have a rails 3 application where a user has_many addresses. I'm trying to make a form in which a user can add fields to create new addresses by clicking on a plus icon. The issue is that I can't add fields with correct name into my simple_form with javascript.
The fields are well added to my view, but they all have the same name : user[addresses_attributes][0][name]
instead of user[addresses_attributes][0 to x number][name]
. Because of that, I can't save new addresses... Can someone help me ?
my edit_addresses.html.erb :
<%= simple_form_for(current_user) do |f| %>
<% current_user.addresses.each do |address| %>
<div class="address_preview">
<%= f.simple_fields_for :addresses, address do |a| %>
<%= a.input :name %>
<%= a.input :description %>
<% end %>
</div>
<% end %>
<% end %>
<a href='<%= new_address_path(current_user) %>' data-remote='true'>Add</i></a>
When a user click on "Add" he is redirect_to my new.js.html file:
$('#addresses_container').append('<%= j render partial: 'new_address', locals: { target: @address } %>');
And the 'new_address' partial is:
<div class="address_preview">
<%= simple_form_for(current_user) do |f| %>
<% f.simple_fields_for :addresses, target do |t| %>
<%= t.input :name %>
<%= t.input :description %>
<% end %>
<% end %>
</div>
这个答案很长,但是在Railscasts#197中 。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.