[英]How to validate attachment in nested model?
我有一個設計模型,在兩個關聯(full_image和預覽)中有兩個回形針附件。 我希望僅當full_image和Preview都具有有效文件但似乎無法使其工作時才能保存設計。 現在這是我期望的工作方式,但是當我提交表單時,它不僅不驗證附件。
class Design < ActiveRecord::Base
has_one :full_image, :as => :assetable, :class_name => "FullImage", :dependent => :destroy
has_one :preview , :as => :assetable, :class_name => "Preview" , :dependent => :destroy
accepts_nested_attributes_for :full_image, :preview
validates_associated :preview, :full_image
end
class Asset < ActiveRecord::Base
belongs_to :assetable, :polymorphic => true
delegate :url, :to => :attachment
end
class FullImage < Asset
has_attached_file :attachment
validates_attachment_presence :attachment
end
class Preview < Asset
has_attached_file :attachment
validates_attachment_presence :attachment
end
有人可以建議我應該做什么嗎?
嘗試:在關聯的模型中驗證:attachment,:presence => true而不是validates_attachment_presence
這是我如何工作的
class Design < ActiveRecord::Base
has_one :full_image, :as => :assetable, :class_name => "FullImage", :dependent => :destroy
has_one :preview , :as => :assetable, :class_name => "Preview" , :dependent => :destroy
accepts_nested_attributes_for :full_image, :preview
validates_presence_of :preview
validates_presence_of :full_image
end
class Asset < ActiveRecord::Base
belongs_to :assetable, :polymorphic => true
delegate :url, :to => :attachment
end
class FullImage < Asset
has_attached_file :attachment
end
class Preview < Asset
has_attached_file :attachment
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.