nested_form gem add works但删除失败...为什么? [英] nested_form gem add works but remove fails...why?

查看:145
本文介绍了nested_form gem add works但删除失败...为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是来自GitHub的Ryan Bates的nested_form gem的madebydna版本(可以在 https:// github.com/madebydna/nested_form )。我使用的是支持jQuery的版本,而不是Prototype。



更新:
检查页面底部的更新。 / p>

编辑:

我正在使用:RoR v3.0.5,jQuery v1.4.3,Ruby v1.9.2

END EDIT

编辑:
它出现了传递给pictures_attributes参数的当前结构服务器造成了问题,至少从我在其他网站上看到的问题。当前发送的结构如下:

 pictures_attributes=> {0=> {_ destroy= >1,id=>5}} 

已经看到,pictures_attributes错误具有以下结构:

 pictures_attributes=> [{_ destroy=> 1,id=>5}] 

改变我的代码,使后者发生,而不是前者。想法?



END编辑

我遵循T的指示,添加链接对于添加嵌套非常有用模型,但删除失败。换句话说,我可以为特定模型添加多个字段,并且在我提交表单时创建和保存这些模型,但是当我再次编辑相同的记录并去除刚创建的嵌套模型时,它们会失败从相关记录中删除。



以下是安装nested_form gem后生成的nested_form.js脚本。原谅,但我创建了一个Pastie并减少了它: http://bit.ly/ge5BO7



下面是相关的模型代码:

  has_many:pictures,:as => :可成像,:依赖=> :销毁
accept_nested_attributes_for:图片,:allow_destroy => true,:reject_if => lambda {| a |一[:照片] .blank? }

以下是视图代码(在_form.html.erb部分中):

 < div id =picture_fields> 
<%f.fields_for:pictures do | pics | %GT;
< div class =field>
<%= pics.label:照片,照片%>
<%= pics.file_field:photo%>
<%= pics.link_to_remove删除照片%>
< / div>
<%end%>
< p>
<%= f.link_to_add添加照片,:图片%>
< / p>

以下是为一个字段生成的HTML:

 < div class =fields> 
< div class =field>
< label for =equipment_pictures_attributes_0_photo>照片< / label>
< input id =equipment_pictures_attributes_0_photoname =equipment [pictures_attributes] [0] [photo]type =file>
< input id =equipment_pictures_attributes_0__destroyname =equipment [pictures_attributes] [0] [_ destroy]type =hiddenvalue =false>
< a href =javascript:void(0)class =remove_nested_fields>删除照片< / a>
< / div>
< input id =equipment_pictures_attributes_0_idname =equipment [pictures_attributes] [0] [id]type =hiddenvalue =5>
< / div>

以下是提交表单时生成的日志条目:

 开始POST< / equipment / 494882120>< ipadd deleted>在2011-03-24 13:04:18 -0400 
通过EquipmentController进行处理#以HTML格式更新
参数:{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=> { => {_ destroy=>1,id=>5}}},commit=>更新设备,id=>494882120}
设备负载(0.7ms)选择设备。*从设备WHERE设备。id= 494882120限制1
图片加载(0.4ms)选择图片在哪里图片。IDIN(5 )AND(pictures.imageable_id = 494882120 ANDpictures.imageable_type ='Equipment')
DEPRECATION警告:在您的模型中覆盖验证已被弃用,请使用Base#validate:method_name代替。 (从/opt/intranet3-dev/app/controllers/equipment_controller.rb:63更新的块中调用)
部门负载(0.1ms)SELECTdepartments。* FROMdepartmentsWHEREdepartments。 id= 320903175 LIMIT 1
条件加载(0.1ms)SELECTconditions。* FROMconditionsWHEREconditions。id= 746868371 LIMIT 1

现在,就我而言,我覆盖了所有的基础,允许在_destroy =>'1'时销毁嵌套对象在提交的参数中遇到。但是,该对象显然不会被破坏。参数提交给服务器的方式是否存在错误?

希望有人比我更敏锐的眼睛会看到一些明显的错误。帮助!

更新:

好的,我设法解决了这个问题:reject_if子句从我的accep_nested_attributes_for语句。这是一个附带案件,如果你有一个上传文件的形式,我敢说它是一个bug。



在我的情况下,我能够将照片附加到我的设备模型,并且在编辑它时会渲染这样的格式,即对于我附加到我的记录中的每张照片,文件字段都是空白的(这是应该发生的情况)。在这个东西插上几个小时之后,我考虑了:reject_if子句是否会导致控制器操作完全忽略嵌套记录,即使我告诉它它会销毁该记录。



如果您有:reject_if子句,再加上:allow_destroy => true,并且:reject_if的计算结果为true您的记录不会被破坏无论什么

我正在尝试修改保留我的:reject_if功能的方法。



干杯,
Les。

解决方案

问题是,嵌套状态记录在提交时会导致 acceptable_nested_attributes_for 语句中的:reject_if 子句评估为true,从而告诉控制器跳过它并对其执行任何操作。因此,嵌套记录不会被销毁,因为没有对其执行任何操作。


I'm using the madebydna version of Ryan Bates' nested_form gem from GitHub (can be found at https://github.com/madebydna/nested_form). I'm using the forked version for jQuery support as opposed to Prototype.

UPDATE: Check the bottom of the page for an update to this question.

EDIT:

I'm using: RoR v3.0.5, jQuery v1.4.3, Ruby v1.9.2

END EDIT

EDIT: It appears the current structure of the pictures_attributes parameter passed along to the server is causing an issue, at least from what I've seen on other websites. The currently sent structure is as follows:

"pictures_attributes"=>{"0"=>{"_destroy"=>"1", "id"=>"5"}}

All other sites I've seen, the pictures_attributes error had the following structure:

"pictures_attributes"=>[{"_destroy"=>"1", "id"=>"5"}]

I'm not sure how to alter my code to make it so the latter occurs, rather than the former. Thoughts?

END EDIT

I followed the directions to a T, the add link works great for adding nested models, but remove fails. In otherwords, I can add multiple fields for a particular model, and those models get created and saved when I submit the form, but when I go to edit the same record again and go to remove the nested models which I just created, they fail to be removed from the associated record.

Here's the nested_form.js script that gets generated after installing the nested_form gem. Forgive but I created a Pastie and reduced it: http://bit.ly/ge5BO7

Here is the pertinent model code:

has_many :pictures, :as => :imageable, :dependent => :destroy
accepts_nested_attributes_for :pictures, :allow_destroy => true, :reject_if => lambda { |a| a[:photo].blank? }

Here is the view code (in the _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>

Here's the generated HTML for one field:

<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>

And here's the log entry that gets generated when submitting the form:

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

Now, as far as I'm concerned I'm covering all of my bases with allowing the nested objects to be destroyed when _destroy => '1' is encountered in the submitted parameters. However, the object is clearly not being destroyed. Is there an error in the way the parameters are being submitted to the server?

Hopefully someone with a keener eye than I will see some glaring error. Help!

UPDATE:

Okay, I've managed to resolve the problem by taking out the :reject_if clause from my accepts_nested_attributes_for statement. This is a fringe case in the event you have an upload file form, and I dare call it a bug.

In my case I was able to attach photos to my Equipment model, and when editing it would render the form such that the file field was blank (which is what's supposed to happen) for every photo I have attached to my record. After plugging away at this thing for hours, I pondered if the :reject_if clause was causing the controller action to ignore the nested record entirely, even though I told it to destroy the record. Well, it does.

If you have a :reject_if clause, coupled with a :allow_destroy => true, and if the :reject_if evaluates to true YOUR RECORD WILL NOT BE DESTROYED NO MATTER WHAT

I'm currently trying to hack out a method for retaining my :reject_if functionality.

Cheers, Les.

解决方案

The problem is that the state of the nested record, when submitted, is causing the :reject_if clause in the accepts_nested_attributes_for statement to evaluate to true, thus telling the controller to skip over it and perform no action on it. Hence, the nested record is not destroyed since no action is performed on it.

这篇关于nested_form gem add works但删除失败...为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆