accepts_nested_attributes_for rails 4未删除

我现在已经阅读和研究了大约3天。 这是我的最后一招。

land.rb:

has_many :uploads , :dependent => :destroy accepts_nested_attributes_for :uploads, :allow_destroy => true,:reject_if => :all_blank 

upload.rb

 belongs_to :land 

_land_form_partial.html.erb

  {:multipart => true} do |f| %>  

Delete:
#... buttons and other fields

lands_controller.rb

 def update if @land.update_attributes(land_params) flash[:success] = "Land updated" redirect_to lands_path else flash[:alert] = @land.errors.full_messages.first redirect_to edit_land_path end end def land_params params.require(:land).permit( uploads_attributes: [ :id, :filename ] ) end 

当我向文本字段添加内容并更新它时,所有更新都正确。 如果我单击复选框,它将不会删除该字段。

有人可以为此阐明一下吗?

此外,我尝试了awesome_nested_fields仍然一切正常,除了删除实际记录。

先感谢您。

编辑:解决方案:(我喜欢把解决方案放在问题中以防万一有人想在移动设备上查看它,因为我讨厌当我无法立即看到解决方案时)

感谢@nTraum

 def land_params params.require(:land).permit( uploads_attributes: [ :id, :filename, :_destroy ] ) end 

一切都会花花公子:)

您还需要为嵌套模型允许:_destroy参数,因为当您选中表单中的“删除”复选框时会使用此参数。 这是Rails标记必须销毁的模型实例的方式。

 def land_params params.require(:land).permit(uploads_attributes: [:id, :filename, :_destroy]) end 

OP与我没有相同的问题,但对于遇到这个问题的人来说,对我来说,缺少allow_destroy: true作为模型中的accepts_nested_attributes调用的参数。