Custom wrapper: link_to_add for new table row

842 views
Skip to first unread message

Alexander Schomburg

unread,
Jun 11, 2012, 2:22:56 PM6/11/12
to SimpleForm
Dear mailing list,

As requested by rafaelfranca in my GitHub issue -
https://github.com/plataformatec/simple_form/issues/590 - I'm going to
repost my question here. He asked me to attach the code for
link_to_add, but I'm uncertain about what he's asking for. I'm using
the provided link_to_add method from nested_form / simple_form.

I'm having issues configuring a custom wrapper so that link_to_add
automatically adds a new <tr> to an existing table. Here is my current
(simplified) markup:
---
<%= simple_nested_form_for @user, :html => {:class => "form-
horizontal"} do |f| %>
<%= f.input :username %>
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Description</th>
<th colspan="2">Label</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<%= f.simple_fields_for :user_events, :wrapper => :event_table
do |e| %>
<tr>
<td><%= e.input_field :date, :as => :date_month_year %></td>
<td><%= e.input_field :description %></td>
<td><%= e.input_field :label %></td>
<td><%= e.input_field :label_class %></td>
<td><%= e.link_to_remove "Delete this event." %></td>
</tr>
<% end %>
</tbody>
</table>
<%= f.link_to_add :user_events, "Add new event" %>
<% end %>
---

This is my custom wrapper from config/initializers/simple_form.rb:
---
config.wrappers :event_table, :tag => 'tr' do |b|
b.use :html5
b.use :placeholder
b.wrapper :tag => :td do |input|
input.use :input
end
end
---

The default wrapper used for the main form (not the nested one)
is :bootstrap.

I already restarted the server several times, but I always experience
that the new fields added by link_to_addget wrapped wrong and at the
wrong position in the markup.

I'm looking forward to your help. With best regards,

Alex

Rafael Mendonça França

unread,
Jun 11, 2012, 2:35:14 PM6/11/12
to plataformate...@googlegroups.com
link_to_add is not a SimpleForm method.

Looking at the code I saw that nested_form will not work as you want because it is not using simple_fields_for to create the new form fields, so the wrapper option will not be used. You can see this code here: https://github.com/ryanb/nested_form/blob/master/lib/nested_form/builder_mixin.rb#L24

Alexander Schomburg

unread,
Jun 11, 2012, 2:42:53 PM6/11/12
to SimpleForm
Thanks for your answer. Again my fault, you're right SimpleForm
doesn't provide any link_to_add method. Too bad that the one provided
by nested_form is incompatible with the wrappers. Besides, I think I'm
lacking the knowledge to build my own method that works with the
wrapper API.

On 11 Jun., 20:35, Rafael Mendonça França <rafaelmfra...@gmail.com>
wrote:
> link_to_add is not a SimpleForm method.
>
> Looking at the code I saw that nested_form will not work as you want
> because it is not using simple_fields_for to create the new form fields, so
> the wrapper option will not be used. You can see this code here:https://github.com/ryanb/nested_form/blob/master/lib/nested_form/buil...
>
> Rafael Mendonça Françahttp://twitter.com/rafaelfrancahttps://github.com/rafaelfranca
>
> On Mon, Jun 11, 2012 at 3:22 PM, Alexander Schomburg <a...@schomb.org>wrote:
>
>
>
>
>
>
>
> > Dear mailing list,
>
> > As requested by rafaelfranca in my GitHub issue -
> >https://github.com/plataformatec/simple_form/issues/590- I'm going to

Rafael Mendonça França

unread,
Jun 11, 2012, 11:08:27 PM6/11/12
to plataformate...@googlegroups.com
You can watch this series of screencasts that explain the code of nested_form gem:


You can try to override the link_to_add metod with an implementation that can use the wrappers API.

Rafael Mendonça França
http://twitter.com/rafaelfranca

Alexander Schomburg

unread,
Jun 12, 2012, 7:43:36 AM6/12/12
to SimpleForm
Thanks for your answer. I found a solution in 'cocoon' instead of
'nested_form'. You can find it here:
http://stackoverflow.com/questions/10995932/rails-with-simple-form-add-new-table-row-with-link-to-add/10995935

On 12 Jun., 05:08, Rafael Mendonça França <rafaelmfra...@gmail.com>
wrote:
> You can watch this series of screencasts that explain the code of
> nested_form gem:
>
> http://railscasts.com/episodes/73-complex-forms-part-1http://railscasts.com/episodes/74-complex-forms-part-2http://railscasts.com/episodes/75-complex-forms-part-3
>
> You can try to override the link_to_add metod with an implementation that
> can use the wrappers API.
>
> On Mon, Jun 11, 2012 at 3:42 PM, Alexander Schomburg <a...@schomb.org>wrote:
>
>
>
>
>
>
>
> > Thanks for your answer. Again my fault, you're right SimpleForm
> > doesn't provide any link_to_add method. Too bad that the one provided
> > by nested_form is incompatible with the wrappers. Besides, I think I'm
> > lacking the knowledge to build my own method that works with the
> > wrapper API.
>
> > On 11 Jun., 20:35, Rafael Mendonça França <rafaelmfra...@gmail.com>
> > wrote:
> > > link_to_add is not a SimpleForm method.
>
> > > Looking at the code I saw that nested_form will not work as you want
> > > because it is not using simple_fields_for to create the new form fields,
> > so
> > > the wrapper option will not be used. You can see this code here:
> >https://github.com/ryanb/nested_form/blob/master/lib/nested_form/buil...
>
> > > Rafael Mendonça Françahttp://
> > twitter.com/rafaelfrancahttps://github.com/rafaelfranca
>
> > > On Mon, Jun 11, 2012 at 3:22 PM, Alexander Schomburg <a...@schomb.org
> > >wrote:
>
> > > > Dear mailing list,
>
> > > > As requested by rafaelfranca in my GitHub issue -
> > > >https://github.com/plataformatec/simple_form/issues/590-I'm going to
Reply all
Reply to author
Forward
0 new messages