I am using Mongoid 2.4.12 and Rails 3.2.8 with cocoon. My nested form appears to work flawlessly on the front end, but the nested model and relation are not being saved.
Project
include Mongoid::Document
include Mongoid::Paranoia
include Mongoid::Timestamps
belongs_to :user
has_many :tasks
accepts_nested_attributes_for :tasks, allow_destroy: true
attr_accessible :name, :completed, :tasks_attributes
field :name
field :completed, type: Boolean
validates_presence_of :name
Task
include Mongoid::Document
include Mongoid::Paranoia
include Mongoid::Timestamps
belongs_to :user
belongs_to :category
belongs_to :projects
accepts_nested_attributes_for :category, allow_destroy: false
attr_accessible :title, :description
field :title
field :description
Project View
<%= form_for(@project) do |f| %>
<% if @project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% @project.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="row-fluid field-set">
<div class="field">
<%= f.text_field :name, placeholder: 'Name' %>
</div>
<div class="buttonset">
<%= f.radio_button :completed, true, checked: true, class: 'completed-button inline' %>
<%= label :completed, 'Completed', value: true, class: 'inline' %>
</div>
</div>
<div class="row-fluid tasks-container">
<%= link_to_add_association 'Add task', f, :tasks, class: 'btn', id: 'task-button' %>
<%= f.fields_for :tasks do |task| %>
<%= render 'task_fields', f: task %>
<% end %>
</div>
<%= f.submit 'Save', class: 'btn', id: 'save-button' %>
<% end %>
Task Partial
<div class="new_task">
<div class="nested-fields task-container">
<%= link_to_remove_association 'x', f, class: 'close' %>
<div class="field-set">
<%= f.collection_select(:category, Category.all, :id, :name, { prompt: 'Task Type' }, { class: 'category-selector' } ) %>
</div>
<div class="field-set">
<%= f.text_field :name, placeholder: 'Name', class: 'name-field' %>
</div>
<div class="field-set">
<%= f.text_field :description, placeholder: 'Description', class: 'description-field' %>
</div>
</div>
</div>
Here are my params:
Started POST "/projects" for 127.0.0.1 at 2012-10-29 01:36:01 -0500
Processing by ProjectsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"rNX8DvKuEJgjxBvJ5c5ArTjtIMR5Uy6DtCCou0wHswk=", "project"=>{"name"=>"Example Name", "completed"=>"true", "tasks_attributes"=>{"1351491343724"=>{"_destroy"=>"", "category"=>"5089e01023499d9904000004", "title"=>"Example Title", "description"=>"Example description."}}}, "commit"=>"Save"}
MONGODB myapp_development['users'].find({:deleted_at=>nil, :_id=>BSON::ObjectId('5089a25523499d92d8000004')}).sort([[:_id, :asc]])
MONGODB myapp_development['categories'].find({:deleted_at=>nil, :_id=>BSON::ObjectId('5089e01023499d9904000004')}).sort([[:_id, :asc]])
MONGODB myapp_development['projects'].insert([{"_id"=>BSON::ObjectId('508e23d123499d2a6c00000b'), "name"=>"Example Name", "completed"=>true, "user_id"=>BSON::ObjectId('5089a25523499d92d8000004'), "updated_at"=>2012-10-29 06:36:01 UTC, "created_at"=>2012-10-29 06:36:01 UTC}])
Redirected to http://localhost:3000/projects/508e23d123499d2a6c00000b
Completed 302 Found in 24ms
As you can see, I am not getting any errors regarding accepts_nested_attributes_for
, etc.
In my projects#create
action, I can debug after @project.save
and see that @project.tasks
correctly returns an array of one task. However this task is never created/inserted/saved, and there is no relation afterward. Any suggections on what I might be missing?
According to the Mongoid relation docs, child elements are not automatically persisted. I am unclear on why mine are not, since accepts_nested_attributes_for
should resolve this. Regardless, setting autosave: true
on the relation will resolve my original problem. And for the record, cocoon will also work with has_and_belongs_to_many
. Brilliant!