【发布时间】:2015-06-12 05:28:32
【问题描述】:
我可以在回形针模型上放置多个图像吗?我该怎么说?
rails g 迁移 add_image_to_model_name
def self.down remove_attachment :model_name, :image, :image2 结尾 def self.up
remove_attachment :model_name, :image, :image2
end
结束
【问题讨论】:
我可以在回形针模型上放置多个图像吗?我该怎么说?
rails g 迁移 add_image_to_model_name
def self.down remove_attachment :model_name, :image, :image2 结尾 def self.up
remove_attachment :model_name, :image, :image2
end
结束
【问题讨论】:
最好的方法是为回形针附件创建新表并在此表和您的父/现有表之间设置has-many 关联。
使用这种方法,您可以根据需要上传任意数量的图片。
# app/models/gallery.rb
class Gallery < ActiveRecord::Base
has_many :pictures, :dependent => :destroy
end
创建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
这里有教程可以参考:Adding multiple images to a Rails model with paperclip
【讨论】: