Search code examples
ruby-on-railssimple-formnested-formscocoon-gem

Rails simple_form Nested Model Fields Won't Render


I'm using Rails 3.2 and simple_form to build an application. I'm trying to have each nested model display it's fields in a different tab using Twitter Bootstrap. From reading some stack overflow questions people seemed to recommend the cocoon gem which I've installed too. I'm trying to follow the example on the cocoon site (https://github.com/nathanvda/cocoon) but for some reason it's not working. (Could be a problem in my translation of slim to standard Rails).

Here is my code, I'm doing my best to strip out pieces that aren't relevant.

Here are my Models:

class SuperRequest < ActiveRecord::Base
attr_accessible (all of my fields)
has_many :prisms
accepts_nested_attributes_for :prisms

class Prism < ActiveRecord::Base
attr_accessible :access, :business_client, :sla
belongs_to :super_request

My form is as follows:

<%= simple_form_for @super_request, :html => {:class => 'form-horizontal'} do |f| %>
<div class="tabbable"> <!-- Only required for left/right tabs -->
 <ul class="nav nav-tabs">
  <li class="active"><a href="#tab1" data-toggle="tab">Basic Information</a></li>
  <li><a href="#tab2" data-toggle="tab">Prism</a></li>
  <li><a href="#tab3" data-toggle="tab">Computer</a></li>
  <li><a href="#tab4" data-toggle="tab">Software</a></li>
  <li><a href="#tab5" data-toggle="tab">IM</a></li>
 </ul>
<div class="tab-content">
<div class="tab-pane active" id="tab1">
  <%= f.input :ntid, label: 'NTID' %>
  (a lot more fields)
</div>

<div class="tab-pane" id="tab2">
  <p> Content for PRISM </p>
    <%= f.simple_fields_for :prisms do |prism| %>
      <%= render 'prism_fields', :f => prism %>
    <% end %>
</div>

I've created a partial for the prism fields. Here is what I have in that partial.

<%= .nested-fields %>
<%= f.inputs do %>
  <%= f.input :access %>
  <%= f.input :business_client %>
  <%= f.input :sla %>
<% end %>

The page renders and my hard coded text "Content for PRISM" shows up. But, the fields for prism do not.

All help is greatly appreciated.


Solution

  • Are you build any Prism objects?
    For example:
    1) <% @super_request.prisms.build %>
    OR several objects:
    2)<% 3.times {@super_request.prisms.build} if @super_request.prisms.empty? %>

    Update: For example, try to paste after :

    <p> Content for PRISM </p>
    

    This line of code:

    <% @super_request.prisms.build %>