Mongoid 2.4.12 と Rails 3.2.8 をcocoonで使用しています。ネストされたフォームはフロント エンドで問題なく動作しているように見えますが、ネストされたモデルとリレーションが保存されていません。
計画
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
仕事
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
プロジェクト ビュー
<%= 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 %>
タスクの一部
<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>
ここに私のパラメータがあります:
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
ご覧のとおりaccepts_nested_attributes_for
、などに関するエラーは発生していません。
私のprojects#create
アクションでは、後でデバッグして、1 つのタスクの配列が正しく返される@project.save
ことを確認できます。@project.tasks
ただし、このタスクは作成/挿入/保存されることはなく、その後の関係はありません。私が見逃している可能性のあるものについての提案はありますか?