I have a form containing a nested model.
<%= simple_form_for(visit) do |f| %>
<%= f.error_notification %>
<%= f.input :visit_date, :label => 'Besøksdato', :as
=> :string, :input_html => { :class => 'date_picker', :size => 10 } %>
<fieldset id="visit-form">
<legend>Kontaktpersoner på arbeidsplassen</legend>
<%= f.simple_fields_for :workplace_contacts do |contacts_form| %>
<div id=<%=dom_id(contacts_form.object)%> class="visit-contact">
<%= contacts_form.input :name, :label =>
contacts_form.object.workplace_role.name, :label_html=>{:class =>
"role"} %>
<%= contacts_form.input :email, :input_html => {:size => 40} %>
<%= contacts_form.input :phone, :input_html => {:size => 15} %>
<%= contacts_form.input :workplace_role_id, :as
=> :hidden, :input_html => { :value =>
contacts_form.object.workplace_role.id } %>
<%= link_to 'Fjern kontakt',
visit_workplace_contact_path(visit, contacts_form.object), confirm:
'Er du sikker?', method: :delete, :remote => true %>
</div>
<% end %>
</fieldset>
<br/>
<%= f.submit 'Lagre'%>
<% end %>
As you can see I surround the three input fields in a div: <div id=<
%=dom_id(contacts_form.object)%> class="visit-contact">
The reason for this is that I want to be able to add and remove the
content of the div with ajax calls.
Each of the div's gets an id such like this:
<div id="workplace_contact_58" class="visit-contact">
…
…
</div>
but simple_form also generates this hidden input outside my
workplace_contact_58 div:
<input id="visit_workplace_contacts_attributes_1_id"
name="visit[workplace_contacts_attributes][1][id]" type="hidden"
value="58">
So, when I delete <div id="workplace_contact_58" class="visit-
contact"> from the DOM (and DB), the simple_form generated content
containing the id of the now deleted record are still there.
If now I add a new "row" and then hit save, the record "58" is still
found in the DOM, and passed as parameters to the update method, but
the update fails with a record not found exception.
Is there a way that I wrap the content of each "generated row content"
in a div so that they are grouped together?
André