Rails嵌套对象表单与Paperclip - paperclip attrs被reject_if忽略

时间:2009-08-21 17:10:57

标签: ruby-on-rails ruby paperclip

提交嵌套对象表单时,我无法拒绝它,因为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"=>""}

我认为这是因为它只查看属性列,但不是它,正如我通过向资产添加attr_accessor所发现的那样

p attrs
{"misc_attr"=>"", "identifier"=>""}

我可以继续使用奇怪的ins&amp;但是我想把它拿出去看看之前有谁遇到过这个问题。

1 个答案:

答案 0 :(得分:0)

我不知道这是不是重点,但不应该在Asset类中有一行声明与Stage类的关联,如:

    class Asset < ActiveRecord::Base
    has_and_belongs_to_many :stages

...