Ruby on rails 3.1 Mongoid\u destroy不会使用嵌套表单gem删除嵌入文档
我在Rails 3.1、Mongoid 2.3.3上,使用嵌套的_表单gem。在我的表单中,我设置了嵌套的表单link-to-add和link-to-remove来添加和删除模型中的嵌入文档。link\u-to\u-add-helper方法非常有效,但是link\u-to\u-remove-help方法的更改不会在MongoDB中持久化。在rails输出中,我可以看到传递给Mongoid的JSON参数设置了_destroy:1值,但更改没有保存到MongoDB 模型如下:Ruby on rails 3.1 Mongoid\u destroy不会使用嵌套表单gem删除嵌入文档,ruby-on-rails-3.1,mongoid,nested-forms,Ruby On Rails 3.1,Mongoid,Nested Forms,我在Rails 3.1、Mongoid 2.3.3上,使用嵌套的_表单gem。在我的表单中,我设置了嵌套的表单link-to-add和link-to-remove来添加和删除模型中的嵌入文档。link\u-to\u-add-helper方法非常有效,但是link\u-to\u-remove-help方法的更改不会在MongoDB中持久化。在rails输出中,我可以看到传递给Mongoid的JSON参数设置了_destroy:1值,但更改没有保存到MongoDB 模型如下: class MenuI
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
以下是控制器的更新方法:
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中,鸡肉文件仍然存在;该文档也会显示在在线视图中(该页面会提取嵌入文档中的所有项目)
我确信我遗漏了一些东西,但我一直无法弄清楚为什么没有删除嵌入的文档。我遇到了同样的问题- 决心
accepts_nested_attributes_for :phones, :allow_destroy => true
是的,您的
:allow\u destory
应该是:allow\u destroy