【问题标题】:paperclip polymorphic association and saving回形针多态关联与保存
【发布时间】:2010-02-18 13:28:17
【问题描述】:

我将我的 photo.rb 模型更改为多态模型,并且可用于需要保存图像的各种其他模型,它工作正常,但我无法弄清楚如何通过父模型正确保存新附件。有任何想法吗?我是否必须以不同的方式处理这个问题?因为,它也没有得到 imageable_type ......我必须添加它。

我看到了多态回形针插件,它看起来像一只野兽并使用了另一个表?所以我希望能有更好的解决方法。

我明白了:

ActiveRecord::AssociationTypeMismatch (Photo(#2189565500) expected, got Array(#2148226700))

Parameters: {"commit"=>"Create", "action"=>"update", "_method"=>"put", "authenticity_token"=>"kp7NeMs7moGwu0AZMXVowUpphp9vzitdZZ6t8YO7RKQ=", "id"=>"2586", "muffin"=>{"photos"=>{"data"=>#<File:/var/folders/4d/4dqp9CQQEbmM1akqbtLxzk+++TI/-Tmp-/RackMultipart20100218-11622-onqem2-0>}}, "controller"=>"muffins"}

松饼.rb

has_many :photos, :as => :imageable, :dependent => :destroy

照片.rb

belongs_to :imageable, :polymorphic => true
has_attached_file :data,
    :storage => 's3',
    :s3_credentials.....

【问题讨论】:

    标签: ruby-on-rails ruby polymorphism paperclip polymorphic-associations


    【解决方案1】:

    我有了使用accepts_nested_attributes_for 的想法,因为如果它不是多态关联而是我正在向另一个模型抛出属性时我可能会这样做......瞧,它奏效了。

      has_many :photos, :as => :imageable, :dependent => :destroy
      accepts_nested_attributes_for :photos
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多