samedi 25 avril 2015

Changing a wrapper tag in SimpleForm


I've read the docs on how to customize wrapper's globally in SimpleForm but what I'd like to do is change the wrapper specially for one view and the partial inside of it. Right now the partial is wrapped in a div with the class name nested-fields.

With the use of cocoon I'm also dynamically inserting new inputs that are also wrapped in divs.

Simply put I need to change nested-fields div into a table and change the inserted inputs, that have a class of input into trs with the inputs wrapped inside tds. This would look something like this:

<table class="track-form nested-fields">
  <tr><td><%= f.input :disc %></td></tr>
  <tr><td><%= f.input :number %></td></tr>
  <tr><td><%= f.input :name %></td></tr>
</table>

Unfortunately manually changing these tags is only ignored when the views are rendered. Here's what the view code currently looks like without the table elements added:

Form View

<%= simple_form_for(@album) do |f| %>
  <div class="form-inputs">
    ...
  </div>

  <div class="form-inputs">
    <%= f.simple_fields_for :tracks, tag: "table" do |track| %>
      <%= render 'track_fields', :f => track %>
    <% end %>

    <div id='links'>
      <%= link_to_add_association 'Add a Track', f, :tracks %>
    </div>
  </div>

Partial

<div class="track-form nested-fields">
  <%= f.input :disc %>
  <%= f.input :number %>
  <%= f.input :name %>

  <%= link_to_remove_association "remove track", f %>
</div>


Aucun commentaire:

Enregistrer un commentaire