samedi 25 avril 2015

Changing the wrapper tag for inserted elements in Cocoon for Rails


I was wondering if it were at all possible to change the element type of the wrapper for newly inserted inputs. Going through the docs I was unable to find a way in doing so.

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