accepts_nested_attributes_for rails 4 没有删除 [英] accepts_nested_attributes_for rails 4 is not deleting
问题描述
我已经阅读和研究了大约 3 天了.这是我最后的手段.
land.rb:
has_many :uploads , :dependent =>:破坏accepts_nested_attributes_for :uploads, :allow_destroy =>真,:reject_if =>:all_blank
上传.rb
belongs_to :land
_land_form_partial.html.erb
<%= form_for land , :html =>{:multipart =>真} 做 |f|%><%= f.fields_for :uploads do |builder|%><div class="land_fields"><%= builder.label :filename, "Image" %><%= builder.text_field :filename %><br/>删除:<%= builder.check_box :_destroy %>
<%结束%>#... 按钮和其他字段<%结束%>
lands_controller.rb
def 更新如果@land.update_attributes(land_params)flash[:success] = "土地更新"重定向到lands_path别的flash[:alert] = @land.errors.full_messages.first重定向到 edit_land_path结尾结尾定义土地参数params.require(:land).permit(uploads_attributes: [:id, :filename])结尾
当我向文本字段添加内容并更新它时,所有内容都会正确更新.如果我单击复选框,它不会删除该字段.
有人可以解释一下吗?
我也尝试了 awesome_nested_fields 仍然一切正常,除了删除实际记录.
先谢谢你.
解决方案:(我喜欢将解决方案放在问题中,以防有人想在移动设备上查看它,因为我讨厌我无法立即看到解决方案)
感谢@nTraum
def land_paramsparams.require(:land).permit(uploads_attributes: [:id,:filename,:_destroy])结尾
一切都会好起来的 :)
您还需要为嵌套模型允许 :_destroy
参数,因为在您选中删除"时会用到它表单中的复选框.这是 Rails 标记必须销毁的模型实例的方式.
def land_paramsparams.require(:land).permit(uploads_attributes: [:id,:filename,:_destroy])结尾
I have been reading and researching for about 3 days now. This is my last resort.
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
<%= form_for land , :html => {:multipart => true} do |f| %>
<%= f.fields_for :uploads do |builder| %>
<div class="land_fields">
<%= builder.label :filename, "Image" %>
<%= builder.text_field :filename %> <br/>
Delete: <%= builder.check_box :_destroy %>
</div>
<% end %>
#... buttons and other fields
<% end %>
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
When I add something to the text field and update it, all updates properly. If I click on the check-box it won't remove the field.
Can someone please shed a light on this?
Also I tried awesome_nested_fields still everything works except for removing the actual record.
thank you in advance.
EDIT: Solution: (I like to put the solution in the question in case someone wants to view it on mobile as I hate when I can't see the solution straight away)
Thanks to @nTraum
def land_params
params.require(:land).permit( uploads_attributes: [ :id, :filename, :_destroy ] )
end
And all will be dandy :)
You need to allow the :_destroy
parameter for your nested model as well, as this gets used when you check the 'Delete' checkbox in the form. It's Rails' way of flagging model instances that have to be destroyed.
def land_params
params.require(:land).permit(uploads_attributes: [:id, :filename, :_destroy])
end
这篇关于accepts_nested_attributes_for rails 4 没有删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!