【发布时间】:2011-03-24 17:10:38
【问题描述】:
我正在使用来自 GitHub 的 Ryan Bates 的 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 部分):
<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 => '1' 时,我将覆盖我的所有基础,允许销毁嵌套对象。但是,该对象显然没有被破坏。参数提交到服务器的方式是否有错误?
希望比我更敏锐的人会看到一些明显的错误。救命!
更新:
好的,我已经通过从我的 accept_nested_attributes_for 语句中删除 :reject_if 子句来解决了这个问题。如果您有上传文件表单,这是一个边缘案例,我敢称它为错误。
在我的情况下,我能够将照片附加到我的设备模型,并且在编辑它时会呈现表单,使得我附加到记录中的每张照片的文件字段都是空白的(这是应该发生的)。在处理这个事情几个小时后,我思考 :reject_if 子句是否导致控制器操作完全忽略嵌套记录,即使我告诉它销毁记录。嗯,确实如此。
如果你有一个 :reject_if 子句,再加上一个 :allow_destroy => true,并且如果 :reject_if 计算结果为 true,那么你的记录无论如何都不会被销毁
我目前正在尝试找出一种方法来保留我的 :reject_if 功能。
干杯, 莱斯。
【问题讨论】:
标签: jquery ruby-on-rails-3 rubygems nested-forms