0

我真的是 Rails 新手,目前正在参与一个项目,我必须为音乐新闻开发一个博客,我的问题是:设计具有多个图像的模型的最佳方法是什么?,这些图像必须由用户上传视图 _form 然后能够在内容部分中将它们用作资源。我使用 Paperclip 创建了一个模型 Post ,它有一组图像,但我希望能够添加多个而不需要在我的模型中声明它们,就像 hasmany 关系。是否可以这样做,如果没有,是否可以上传到 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 回答