1
我可以使用'魔术'_destroy
属性,该属性对accepts_nested_attributes_for适用于常规form_for
帮助器吗?allow_destroy in form_for helper - Ruby on Rails
我有一个文件,我通过Paperclip上传,我想要删除它的选项。
我可以使用'魔术'_destroy
属性,该属性对accepts_nested_attributes_for适用于常规form_for
帮助器吗?allow_destroy in form_for helper - Ruby on Rails
我有一个文件,我通过Paperclip上传,我想要删除它的选项。
是的确 - 在Haml这是一个人为的例子。
- form_for @obj, :url => obj_path, :method => :put do |f|
- f.fields_for :sub, f.object.sub do |sub_form|
- unless sub_form.object.new_record?
%span.label= sub_form.label '_delete', 'Remove File'
%span.input= sub_form.check_box '_delete'
和模型:
accepts_nested_attributes_for :sub, :allow_destroy => true
编辑 - 新的答案:是的,你可以,但它是不那么 “神奇”。在保存之前,您需要在要检查的模型上定义自己的虚拟属性。下面是一个(未经测试)的例子,不使用嵌套的属性:
- form_for @obj do |f|
- unless f.object.new_record?
%span.label= f.label 'delete_file', 'Remove File'
%span.input= f.check_box 'delete_file'
和模型:
attr_accessor :delete_file # this is the "virtual attribute" that gets set by the checkbox
before_update :remove_file
def remove_file
self.file = nil if self.delete_file == '1'
end
虚拟属性的更详细说明,请参见Railscasts #167。
我的意思是没有嵌套属性 - “对于常规form_for帮手吗?” – 2010-03-01 07:35:45
Gotcha - 我更新了我的答案,没有使用嵌套属性的例子。 – 2010-03-01 14:24:03
非常感谢你! – 2010-03-03 09:37:21