0

我将 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操作中,我可以在之后进行调试@project.save并看到它@project.tasks正确返回了一个任务的数组。然而,这个任务永远不会被创建/插入/保存,之后也没有关系。关于我可能遗漏的任何建议?

4

1 回答 1

0

根据Mongoid 关系文档,子元素不会自动持久化。我不清楚为什么我的不是,因为accepts_nested_attributes_for应该解决这个问题。无论如何,设置autosave: true关系将解决我原来的问题。并且为了记录,茧也将与has_and_belongs_to_many. 杰出的!

于 2012-10-31T01:57:30.667 回答