【发布时间】:2013-06-25 03:39:39
【问题描述】:
我正在尝试以嵌套形式使用 :_destroy 方法,但它不起作用
有两种型号:
class Setting < ActiveRecord::Base
attr_accessible :category, :name, :setting_items_attributes, :_destroy
attr_accessor :_destroy
has_many :setting_items, :dependent => :destroy
accepts_nested_attributes_for :setting_items, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
class SettingItem < ActiveRecord::Base
attr_accessible :setting_id, :value
belongs_to :setting
end
在控制器中我创建了一个实例:
def edit
@setting = Setting.find(params[:id])
@setting.setting_items.build
end
表格如下所示:
<%= form_for(@setting) do |f| %>
<div class="field">
<%= f.label :category %>
<%= f.text_field :category %>
</div>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<hr>
<h3>Params:</h3>
<%= f.fields_for :setting_items do |s| %>
<span>
<div class="fields">
<%= s.text_field :value %>
<%= s.hidden_field :_destroy %>
<%= link_to_function "delete", "remove_fields(this)"%>
</div>
<% end %>
<div class="actions">
<%= f.submit "Update", :class => "btn btn-primary"%>
</div>
<% end %>
我使用的函数也在这里:
function remove_fields(link){
$(link).prev("input[type=hidden]").val("1");
$(link).parent().fadeOut("slow");
}
所以setting_items表单根本不起作用,它显示更新成功,但实际上没有添加或删除任何内容。
【问题讨论】:
-
可以添加提交表单时传递的params hash吗?
-
尝试使用 $(link).prev("input[type=hidden]").attr("value","1");
标签: ruby-on-rails nested-attributes hidden-field destroy