提交嵌套对象表单时,我无法让它拒绝无效的子对象,因为 reject_if proc 没有看到 Paperclip 属性。
以下是我的模型和表格的相关部分:
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"=>""}
我认为这是因为它只查看列属性,但事实并非如此,正如我通过向 Asset 添加 attr_accessor 发现的那样
p attrs
{"misc_attr"=>"", "identifier"=>""}
我可以继续进行奇怪的来龙去脉,但我想把它拿出来看看谁以前遇到过这个问题。