使用回形针嵌套对象表单 - 回形针attrs被reject_if忽略
问题描述:
提交嵌套对象表单时,我无法让它拒绝无效的子对象,因为reject_if proc没有看到Paperclip属性。使用回形针嵌套对象表单 - 回形针attrs被reject_if忽略
这里是我的模型的相关部分&形式:
class Stage < ActiveRecord::Base
has_and_belongs_to_many :assets, :uniq => true
accepts_nested_attributes_for :assets, :reject_if => lambda { |attrs| attrs['asset'] }
...
end
class Asset < ActiveRecord::Base
has_attached_file :asset, :path => "#{Rails.root}/public/:attachment/:id/:style/:basename.:extension",
:url => ":attachment/:id/:style/:basename.:extension"
validates_attachment_presence :asset
end
- form_for [@campaign, @stage], :html => {:multipart => true} do |f|
....
- f.fields_for :assets do |asset_form|
- field_set_tag do
- if asset_form.object.new_record?
%h4 New Asset
%p
= asset_form.label :asset, "File"
%br
= asset_form.file_field :asset
%p
= asset_form.label :identifier
%br
= asset_form.text_field :identifier
我把一个调试器在reject_if部分:
(rdb:1) p attrs
{"identifier"=>""}
我认为这是因为它仅着眼于那些属性列但这不是,因为我发现通过添加一个attr_accessor资产
p attrs
{"misc_attr"=>"", "identifier"=>""}
我可以继续与奇怪的插入&出口,但我想得到这一点,看看谁有这个问题之前。
答
我不知道这是不是点,而是shouldn't出现在资产类声明联想到舞台类这样一行:
class Asset < ActiveRecord::Base
has_and_belongs_to_many :stages
?