简体   繁体   English

我可以在回形针模型上放多个图像吗

[英]Can i put more than one image on paperclip model

Can i put more than one image on paperclip model? 我可以在回形针模型上放多个图像吗? and how should i put it ? 我该怎么放呢?

rails g migration add_image_to_model_name Rails G迁移add_image_to_model_name

def self.down
        remove_attachment :model_name, :image, :image2
      end

  def self.up
    remove_attachment :model_name, :image, :image2
  end

end

Best way is to create new table for paperclip attachments and setup has-many association between this table and your parent/existing table. 最好的方法是为回形针附件创建新表,并在此表与您的父表/现有表之间建立has-many关联。

With this approach, you can upload images as many you need. 使用这种方法,您可以上传所需数量的图像。

# app/models/gallery.rb
class Gallery < ActiveRecord::Base
  has_many :pictures, :dependent => :destroy
end

Create picture model and migration file, then define paperclip's has_attached_file in picture model. 创建picture模型和迁移文件,然后在picture模型中定义回形针的has_attached_file

# app/models/picture.rb
class Picture < ActiveRecord::Base
  belongs_to :gallery

  has_attached_file :image,
    :path => ":rails_root/public/images/:id/:filename",
    :url  => "/images/:id/:filename"
end

Here is a tutorial you can refer to: Adding multiple images to a Rails model with paperclip 您可以参考以下教程: 使用回形针将多个图像添加到Rails模型中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM