views:

22

answers:

1

Hi everyone!

I've got a nested form like this :

<% form_for setup_training(@training), :url => admin_trainings_path, :html => { :class => :form } do |f| -%>
<!-- begin of form -->
<!-- content of :partial => "day_form" -->
<% f.fields_for :days do |days_form| %>
    <%= render :partial => "admin/days/form_inner", :locals => { :f => days_form }%>    
<% end %>
<!-- end of content of :partial => "day_form" -->
<%=  link_to_function("[+] Ajouter une date", nil, :id => "add-place-and-dates-link") do |page|
    page.insert_html :bottom, "place-and-dates-inner", :partial => "day_form", :locals => { :f => f }
end %>

<% end -%>

The first time i load my page, the first fields_for block has id 0;

The first time i click on "add a date", a new fields_for block is created with id = 1;

Then, everytime i clink on "add a date", a new fields_for block is displayed, but the id stays to 1.

An HTML example may be more explicit, here's the output i've got :

<!-- This part is generated when i display /new, id is 0 -->
<label class="label" for="training_days_attributes_0_place_id">Lieu</label>
<input class="text_field" id="training_days_attributes_0_place_id" name="training[days_attributes][0][place_id]" size="30" type="text">

<!-- This part is generated when i click on "add a new date", id is 1 -->
<label class="label" for="training_days_attributes_1_place_id">Lieu</label>
<input class="text_field" id="training_days_attributes_1_place_id" name="training[days_attributes][1][place_id]" size="30" type="text">

<!-- This part is generated when i click on "add a new date" a second time, id is 1, but it should be 2 --> 
<label class="label" for="training_days_attributes_1_place_id">Lieu</label>
<input class="text_field" id="training_days_attributes_1_place_id" name="training[days_attributes][1][place_id]" size="30" type="text">

Thank you per advance for you help!

A: 

If you have a look in your inline javascript in your page source you'll see that your link_to_function contains static html which will be be inserted into the place-and-dates-inner container. This does not change because it was build on the server and sent to the user.

To fix this you probably would be best to count client side the number of containers that have previously been inserted into the place-and-dates-inner and increment accordingly in your template. This kind of inline link_to_function stuff I always disliked to be honest.

mark
Thank you for your answer.In fact, i tried to use a link_to_remote function with a render update adding a new form field, but i did not know how i could pass my form (f) to it.If you have any suggestion, feel free to enlight me ;-)
Cédric
There's a couple of railscasts on this subject which should help you out. :)http://railscasts.com/episodes?search=nested
mark