我正在使用GitHub的瑞安贝茨的nested_form gem的madebydna版本(可以在https://github.com/madebydna/nested_form找到)。我使用分叉版本来支持jQuery而不是Prototype。
更新: 检查页面底部是否有此问题的更新。
编辑:
我正在使用:RoR v3.0.5,jQuery v1.4.3,Ruby v1.9.2
结束编辑
编辑: 看来,传递给服务器的pictures_attributes参数的当前结构导致了一个问题,至少从我在其他网站上看到的情况来看。当前发送的结构如下:
"pictures_attributes"=>{"0"=>{"_destroy"=>"1", "id"=>"5"}}
我见过的所有其他网站,pictures_attributes错误都有以下结构:
"pictures_attributes"=>[{"_destroy"=>"1", "id"=>"5"}]
我不确定如何改变我的代码以使后者发生,而不是前者。想法?
结束编辑
我按照指示前往T,添加链接非常适合添加嵌套模型,但删除失败。换句话说,我可以为特定模型添加多个字段,并在我提交表单时创建并保存这些模型,但是当我再次编辑同一条记录并去除我刚刚创建的嵌套模型时,它们会失败从相关记录中删除。
这是在安装nested_form gem之后生成的nested_form.js脚本。原谅但我创建了一个Pastie并减少了它:http://bit.ly/ge5BO7
以下是相关的型号代码:
has_many :pictures, :as => :imageable, :dependent => :destroy
accepts_nested_attributes_for :pictures, :allow_destroy => true, :reject_if => lambda { |a| a[:photo].blank? }
以下是视图代码(在_form.html.erb partial中):
<div id="picture_fields">
<% f.fields_for :pictures do |pics| %>
<div class="field">
<%= pics.label :photo, "Photo" %>
<%= pics.file_field :photo %>
<%= pics.link_to_remove "Remove Photo" %>
</div>
<% end %>
<p>
<%= f.link_to_add "Add Photo", :pictures %>
</p>
以下是为一个字段生成的HTML:
<div class="fields">
<div class="field">
<label for="equipment_pictures_attributes_0_photo">Photo</label>
<input id="equipment_pictures_attributes_0_photo" name="equipment[pictures_attributes][0][photo]" type="file">
<input id="equipment_pictures_attributes_0__destroy" name="equipment[pictures_attributes][0][_destroy]" type="hidden" value="false">
<a href="javascript:void(0)" class="remove_nested_fields">Remove Photo</a>
</div>
<input id="equipment_pictures_attributes_0_id" name="equipment[pictures_attributes][0][id]" type="hidden" value="5">
</div>
这是提交表单时生成的日志条目:
Started POST "/equipment/494882120" for <ipadd deleted> at 2011-03-24 13:04:18 -0400
Processing by EquipmentController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"98/R6EYCAFd6HwBjMV6bhnfRo6cT7NqPZ9fJ/VEOKKE=", "equipment"=>{"location_id"=>"", "model"=>"fasdf", "serial_number"=>"", "unh_id"=>"", "doc_id"=>"", "purchase_price"=>"", "date_acquired(1i)"=>"2011", "date_acquired(2i)"=>"3", "date_acquired(3i)"=>"24", "comment"=>"", "vendor_id"=>"", "new_vendor_name"=>"", "department_id"=>"320903175", "new_department_name"=>"", "activity_id"=>"", "new_activity_code"=>"", "condition_id"=>"746868371", "new_condition_name"=>"", "pictures_attributes"=>{"0"=>{"_destroy"=>"1", "id"=>"5"}}}, "commit"=>"Update Equipment", "id"=>"494882120"}
Equipment Load (0.7ms) SELECT "equipment".* FROM "equipment" WHERE "equipment"."id" = 494882120 LIMIT 1
Picture Load (0.4ms) SELECT "pictures".* FROM "pictures" WHERE "pictures"."id" IN (5) AND ("pictures".imageable_id = 494882120 AND "pictures".imageable_type = 'Equipment')
DEPRECATION WARNING: Overwriting validate in your models has been deprecated, please use Base#validate :method_name instead. (called from block in update at /opt/intranet3-dev/app/controllers/equipment_controller.rb:63)
Department Load (0.1ms) SELECT "departments".* FROM "departments" WHERE "departments"."id" = 320903175 LIMIT 1
Condition Load (0.1ms) SELECT "conditions".* FROM "conditions" WHERE "conditions"."id" = 746868371 LIMIT 1
现在,就我而言,我覆盖了所有基础,允许在_destroy =&gt;时销毁嵌套对象。提交的参数中遇到“1”。但是,该物体显然没有被破坏。参数提交给服务器的方式是否有错误?
希望有一个比我更敏锐的人会看到一些明显的错误。救命啊!
更新:
好的,我已经设法通过从accepts_nested_attributes_for语句中取出:reject_if子句来解决问题。如果您有上传文件表单,这是一个边缘情况,我敢称之为错误。
在我的情况下,我能够将照片附加到我的装备模型中,并且在编辑时,它会渲染表单,使得我附加到记录中的每张照片的文件字段都是空白的(这应该是应该发生的)。在把这个东西塞进几个小时后,我思索:reject_if子句是否导致控制器动作完全忽略嵌套记录,即使我告诉它要销毁记录。嗯,确实如此。
如果您有:reject_if子句,请加上:allow_destroy =&gt;是的,如果:reject_if的评估结果为真,你的记录就不会被破坏而无法解决什么
我目前正试图破解保留我的:reject_if功能的方法。
干杯, 莱斯。
答案 0 :(得分:2)
问题是嵌套记录的状态在提交时导致:reject_if
语句中的accepts_nested_attributes_for
子句评估为true,从而告诉控制器跳过它并执行no采取行动。因此,嵌套记录不会被破坏,因为没有对它执行任何操作。