Wrap generated content from simple_form_for in a div ?

348 views
Skip to first unread message

André Michael Bonkowski

unread,
Oct 5, 2011, 2:18:23 PM10/5/11
to SimpleForm
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é

Carlos Antonio

unread,
Oct 5, 2011, 2:35:31 PM10/5/11
to plataformate...@googlegroups.com
This hidden field is not added by SimpleForm, it's added by Rails in the fields_for call, so it knows which nested record to deal with.

To workaround this, you can just add the hidden field by yourself. Rails will see that the hidden field was already added and will skip his own.

Just add something like: <%= contacts_form.hidden_field :id %> inside the <div> you want.


-- 
At.
Carlos Antonio

André Michael Bonkowski

unread,
Oct 5, 2011, 2:47:04 PM10/5/11
to SimpleForm
Ahh, great! I'll try that, thanks.

André

On Oct 5, 8:35 pm, Carlos Antonio <carlosantoniodasi...@gmail.com>
wrote:
> This hidden field is not added by SimpleForm, it's added by Rails in the fields_for call, so it knows which nested record to deal with.
>
> To workaround this, you can just add the hidden field by yourself. Rails will see that the hidden field was already added and will skip his own.
>
> Just add something like: <%= contacts_form.hidden_field :id %> inside the <div> you want.
>
> https://github.com/rails/rails/blob/3-1-stable/actionpack/lib/action_... 
>
> --  
> At.
> Carlos Antonio
>
>
>
>
>
>
>
> On Wednesday, October 5, 2011 at 3:18 PM, André Michael Bonkowski wrote:
> > 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 (http://contacts_form.object.workplace_role.name), :label_html=>{:class =>

André Michael Bonkowski

unread,
Oct 5, 2011, 6:32:35 PM10/5/11
to SimpleForm
Tested, and it works like a charm. Thanks again!

André

On Oct 5, 8:35 pm, Carlos Antonio <carlosantoniodasi...@gmail.com>
wrote:
> This hidden field is not added by SimpleForm, it's added by Rails in the fields_for call, so it knows which nested record to deal with.
>
> To workaround this, you can just add the hidden field by yourself. Rails will see that the hidden field was already added and will skip his own.
>
> Just add something like: <%= contacts_form.hidden_field :id %> inside the <div> you want.
>
> https://github.com/rails/rails/blob/3-1-stable/actionpack/lib/action_... 
>
> --  
> At.
> Carlos Antonio
>
>
>
>
>
>
>
> On Wednesday, October 5, 2011 at 3:18 PM, André Michael Bonkowski wrote:
> > 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 (http://contacts_form.object.workplace_role.name), :label_html=>{:class =>

Carlos Antonio

unread,
Oct 5, 2011, 7:39:22 PM10/5/11
to plataformate...@googlegroups.com
Nice, no problem :)

-- 
At.
Carlos Antonio

Reply all
Reply to author
Forward
0 new messages