【发布时间】:2011-11-02 21:13:10
【问题描述】:
我正在使用 Rails 3.1、Mongoid 2.3.3 并使用 nested_form gem。在我的表单中,我设置了 nested_form link_to_add 和 link_to_remove 以在我的模型中添加和删除嵌入文档。 link_to_add 辅助方法效果很好,但 link_to_remove 帮助方法更改不会在 MongoDB 中持久化。在 rails 输出中,我可以看到传递给 Mongoid 的 JSON 参数设置了 _destroy: 1 值,但更改未保存到 MongoDB。
这是模型:
class MenuItem
include Mongoid::Document
include Mongoid::Timestamps
field :name
attr_accessible :name
embeds_many :ingredient_infos
accepts_nested_attributes_for :ingredient_infos, :allow_destory => true
attr_accessible :ingredient_infos_attributes
end
这是Controller的更新方法:
def update
@menu_item = MenuItem.find(params[:id])
respond_to do |format|
if @menu_item.update_attributes(params[:menu_item])
format.html { redirect_to @menu_item, notice: 'Menu item was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @menu_item.errors, status: :unprocessable_entity }
end
end
end
这是发送到控制器的参数:
{
"utf8"=>"✓",
"authenticity_token"=>"5abAWfFCr7hkzYXBEss75qlq8DMQ0pW5ltGmrgHwPjQ=",
"menu_item"=>
{
"name"=>"Bowl",
"ingredient_infos_attributes"=>
{
"0"=>
{
"ingredient"=>"Rice",
"_destroy"=>"false",
"id"=>"4eb1b0b118d72f1a26000022"
},
"1"=>
{
"ingredient"=>"Chicken",
"_destroy"=>"1",
"id"=>"4eb1b0b118d72f1a26000025"
}
}
},
"commit"=>"Update Menu item",
"id"=>"4eb1b0b118d72f1a2600001f"
}
在 MongoDB 中,Chicken 文档仍然存在;该文档也显示在在线视图中(该页面提取了嵌入文档中的所有项目)。
我确定我错过了什么,但我无法弄清楚为什么嵌入的文档没有被删除。
【问题讨论】:
标签: ruby-on-rails-3.1 mongoid nested-forms