0

私は Rails を初めて使用し、現在、音楽ニュースのブログを作成するプロジェクトに参加しています。私の質問は、次のとおりです。複数の画像を持つモデルを設計する最良の方法は何ですか?この画像は、ユーザーがビュー _form を作成し、それらをコンテンツ セクション内でリソースとして使用できるようにします。Paperclip を使用して一連の画像を持つモデル Post を作成しましたが、モデルでそれらを宣言する必要なく複数を追加できるようにしたいと考えています。そうでない場合、AWS にアップロードして、各ファイルがアップロードされるときにそこから URL を取得することは可能ですか?

これが私が現在持っているモデルです。

    class Post < ActiveRecord::Base
    attr_accessible :caption, :content, :style, :subtitle, :title, :rect_image, :image_one, :image_two, :image_thr
    validates :caption,  :presence => true, :length => { :minimum => 40 }
    validates :title, :presence => true,:length => { :minimum => 20 }, :uniqueness => true
    validates :content, :presence => true,:length => { :minimum => 400 }
    before_save :create_permalink
    has_attached_file :rect_image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
    has_attached_file :image_one, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
    has_attached_file :image_two, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
    has_attached_file :image_thr, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
    #Validacion de Attachments
    #validates :rect_image, :attachment_presence => true
    #validates_with AttachmentPresenceValidator, :attributes => :rect_image
    def to_param
        permalink
    end
    def self.search(search)
        if search
            where 'title LIKE ?', "%#{search}%"
        else
            scoped
        end
    end
    private
    def create_permalink
      self.permalink = title.downcase
    end
end

前もって感謝します

4

1 に答える 1

2

どうですか

class Post < ActiveRecord::Base
  has_many :post_images
  ...
end

class PostImage < ActiveRecord::Base
  belongs_to :post
end

URL から画像を取得する場合は、carrierwave を使用します。

于 2013-07-06T12:09:37.147 に答える