3

I have a nested model (exptype), and I'm using bootstrap tabs to show the has_many relationship between the parent (experiment) and it's children (exptype).

So far my code looks like:

 <div role="tabpanel">
      <!-- Nav tabs -->
      <ul class="nav nav-tabs" role="tablist">
        <% @experiment.exptypes.each do |e| %>
          <li role="presentation"><a href="#exptype<%=e.id%>" aria-controls="exptype<%=e.id%>" role="tab" data-toggle="tab"><%=e.get_name%></a></li>
        <% end %>
      </ul>

      <!-- Tab panes -->
      <div class="tab-content">
        <%= f.fields_for :exptypes do |builder| %>
          <div role="tabpanel" class="tab-pane" id="exptype<%=builder.object.id%>">
            <%=builder.object.id%>
          </div>
        <% end %>
      </div>
    </div>

The tabs themselves work. But the tab-content does not. What happens is for each tab, all the builder.object.id's are shown in each tab, and not in their respective ones.

I believe it's the fields_for loop that is causing the problem. However, I want to be able to have a tab that allows for the addition of a new exptype.

1 Answer 1

2

It should look like:

<div class="tab-content">
  <% @experiment.exptypes.each do |e| %>    
    <div role="tabpanel" class="tab-pane" id="exptype<%=builder.object.id%>">
      <%= f.fields_for :exptypes, e do |builder| %>        
        <%=builder.object.id%>
      <% end %>
    </div>
  <% end %>
</div>

fields_for is not a loop and will only create one div. Note that i also added e to the fields_for call. Take a look at here: http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for

Sign up to request clarification or add additional context in comments.

7 Comments

Added the code, changes can be seen here. However, the behaviour is still the same.
I updated the code. I guess the div should be outside of the loop.
I tried that just before you updated your answer. Same problem!
Can you make a jsfiddle from the generated HTML or post here?
Try with fields_for call inside of the div. I updated the code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.