ruby on rails - PaperClip for Two Models Error? -
i installed paperclip first model , working fine when try add second model error . trying have 2 image uploading 2 models have created. error:
undefined method `image_content_type' #<ioscourse:0x007fd4bb3bfaf0>
this first model (rubycourse.rb):
class rubycourse < activerecord::base acts_as_votable has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" validates_attachment_content_type :image, :content_type => /\aimage\/.*\z/ has_many :reviews end
this second model (ioscourse.rb):
class ioscourse < activerecord::base attr_accessor :image_file_name has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" validates_attachment_content_type :image, :content_type => /\aimage\/.*\z/ end
you should add needed columns (for paperclip) second model well.
paperclip wrap 4 attributes (all prefixed attachment's name, can have multiple attachments per model if wish) , give them friendly front end. these attributes are:
- |attachment|_file_name
- |attachment|_file_size
- |attachment|_content_type
- |attachment|_updated_at
so, need write/run migration add attributes second model:
class addimagecolumnstoioscourse < activerecord::migration def self.up add_attachment :ios_courses, :image end def self.down remove_attachment :ios_courses, :image end end
paperclip provides migration generator generate file:
$ rails generate paperclip ioscourse image
another idea: if you'll have different models attachments, , these attachments have similar logic (validations, methods, ...), it's idea create polymorphic model (ie. attachment) these paperclip logic , associate new model rest of models.
class attachment < activerecord::base belongs_to :attachable, polymorphic: true # paperclip stuff has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" validates_attachment_content_type :image, :content_type => /\aimage\/.*\z/ end class rubycourse < activerecord::base has_one :attachment, as: :attachable end class ioscourse < activerecord::base has_one :attachment, as: :attachable end
Comments
Post a Comment