mardi 4 août 2015

Nested forms using the Cocoon gem in Ruby on Rails 4.2

I'm building a web app using Rails 4.2 and I have a model called Strategy which has a has_many relationship to a Tactic model.

To create a view for Strategy, I need to have many nested Tactics on the form. I've been using the Cocoon gem to develop this view.

So far, I've been able to get the form to work properly for the most part but the issue is that I wanted to add a Type attribute to my Tactic model.

For a given strategy, I want to have 3 tactics of one given type, and 2 models of another.

The issue is that rendering my view, all five models are listed sequentially without any room for putting in my own HTML code around them.

Is there a simple way to separate out which of these are rendered? Right now, my view looks as follows...

<%= form_for @strategy do |f| %>
    <div class="strategy">
        <%= f.label :name %>
        <%= f.text_field :name %>
    </div>

    <div class="tactics">
        <%= f.fields_for :tactics do |i| %>
            <%= render 'tactics_fields', f: i %>
        <% end %>
    </div>

    <div class="buttons">
        <%= f.button "Save Strategy", class: 'button' %>
    </div>
<% end %>

If anyone can offer any advice, it would be greatly appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire