1
提交嵌套對象表單時,我無法讓它拒絕無效的子對象,因爲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"=>""}
我可以繼續與奇怪的插入&出口,但我想得到這一點,看看誰有這個問題之前。