1
votes

I'm building a multi-nested form in rails 3. I'm using the formtastic_cocoon gem, but I don't think that has much bearing on this issue.

I've got users, users have tasks, tasks have steps. The nesting is users>tasks>steps. I can dynamically add and remove the task fields to the user, and the step fields from the tasks.

However, when I submit the form, the user gets tasks, but the task>steps don't get saved to the database.

Rails isn't returning any errors, just nothing happens.

My models are

Class User < ActiveRecord::Base
    acts_as_authentic

    has_many :tasks
        accepts_nested_attributes_for :tasks, :reject_if=> proc {|attributes| attributes[:entry].blank?}, :allow_destroy => true

end

Class Task < ActiveRecord::Base
          attr_accessible :entry

          belongs_to :user
          has_many :steps
         accepts_nested_attributes_for :steps, :reject_if=> proc {|attributes| attributes[:title].blank?}, :allow_destroy => true
end

Class Step < ActiveRecord::Base
        attr_accesible :title

        belongs_to :task
end 

In my form.html.erb I have

<%= semantic_form_for @user %>
    <%= form.inputs :username, :password %>
    <div>
      <% form.semantic_form_fields_for :tasks do |builder| %>
         <%= render 'task_fields', :f=>builder %>
      <% end %>
   <%= link_to_add_association 'add task', form, :tasks %>
   </div>

The _task_fields.html.erb looks like this

 <div class="nested-fields">
     <%= link_to_remove_association "remove task", f %>
        <%= f.inputs :entry %>
          <div>
             <% f.semantic_form_fields_form :steps do |builder| %>
              <%= render 'step_fields' :f => builder %>
             <% end %>
           <%= link_to_add_association 'add step', f, :steps %>
          </div>
</div>

lastly, the _step_fields.html.erb page is

  <div class="nested-fields">
   <%= link_to_remove_association "remove step", f %>
    <%= f.inputs :title %>
  </div>
1

1 Answers

1
votes

Do you see this in the log?:

WARNING: Can't mass-assign protected attributes: steps_attributes

If so, add this to the Task model:

attr_accessible :steps_attributes