gpt4 book ai didi

jquery - nested_form gem 添加有效但删除失败...为什么?

转载 作者:行者123 更新时间:2023-12-01 00:25:26 24 4
gpt4 key购买 nike

我正在使用来自 GitHub 的 Ryan Bates 的nested_form gem 的 madebydna 版本(可以在 https://github.com/madebydna/nested_form 找到)。我使用 fork 版本来支持 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"=>[{"_destroy"=>"1", "id"=>"5"}]

我不知道如何更改我的代码以使其发生后者,而不是前者。想法?

编辑结束

我按照 T 的指示进行操作,添加链接非常适合添加嵌套模型,但删除失败。换句话说,我可以为特定模型添加多个字段,并且在提交表单时创建并保存这些模型,但是当我再次编辑同一记录并删除我刚刚创建的嵌套模型时,它们会失败从关联记录中删除。

这是安装nested_form gem 后生成的nested_form.js 脚本。原谅我创建了一个馅饼并减少了它: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? }

这里是 View 代码(在 _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' 时销毁嵌套对象。然而,该物体显然没有被破坏。参数提交给服务器的方式是否有错误?

希望比我更敏锐的人能看到一些明显的错误。救命!

更新:

好的,我已经通过从accepts_nested_attributes_for 语句中删除 :reject_if 子句来解决这个问题。如果您有上传文件表单,这是一个边缘案例,我敢称其为错误。

就我而言,我能够将照片附加到我的设备模型中,并且在编辑时会渲染表单,使得我附加到记录中的每张照片的文件字段为空白(这是应该发生的情况)。在花费了几个小时的时间后,我思考 :reject_if 子句是否导致 Controller 操作完全忽略嵌套记录,即使我告诉它销毁该记录。嗯,确实如此。

如果您有 :reject_if 子句,再加上 :allow_destroy => true,并且如果 :reject_if 计算结果为 true,则无论如何您的记录都不会被销毁

我目前正在尝试找出一种保留我的 :reject_if 功能的方法。

干杯,莱斯。

最佳答案

问题在于,提交时嵌套记录的状态导致 accepts_nested_attributes_for 语句中的 :reject_if 子句计算为 true,从而告诉 Controller 跳过它并且不对其执行任何操作。因此,嵌套记录不会被破坏,因为没有对其执行任何操作。

关于jquery - nested_form gem 添加有效但删除失败...为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5422881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com