Ruby on rails Rails回形针多态样式
我正在使用回形针作为多个模型的附件,使用的是accepts\u nested\u attributes\u for。有没有办法为每个型号指定特定的回形针样式选项?有。我在站点上使用单表继承(STI),通过资产模型处理音频、视频和图像Ruby on rails Rails回形针多态样式,ruby-on-rails,paperclip,Ruby On Rails,Paperclip,我正在使用回形针作为多个模型的附件,使用的是accepts\u nested\u attributes\u for。有没有办法为每个型号指定特定的回形针样式选项?有。我在站点上使用单表继承(STI),通过资产模型处理音频、视频和图像 # models/Asset.rb class Asset < ActiveRecord::Base # Asset has to exist as a model in order to provide inheritance # It can't
# models/Asset.rb
class Asset < ActiveRecord::Base
# Asset has to exist as a model in order to provide inheritance
# It can't just be a table in the db like in HABTM.
end
# models/Audio.rb
class Audio < Asset # !note inheritance from Asset rather than AR!
# I only ever need the original file
has_attached_file :file
end
# models/Video.rb
class Video < Asset
has_attached_file :file,
:styles => {
:thumbnail => '180x180',
:ipod => ['320x480', :mp4]
},
:processors => "video_thumbnail"
end
# models/Image.rb
class Image < Asset
has_attached_file :file,
:styles => {
:medium => "300x300>",
:small => "150x150>",
:thumb => "40x40>",
:bigthumb => "60x60>"
}
end
最后,由于您有一个资产模型,如果您想要一个包含20个最新资产的列表,您仍然可以直接从中读取。此外,此示例不限于分离媒体类型,它还可用于同一事物的不同类型:Avatar类映像true
是否已附加文件:附件,样式:lambda{
|附件|{
拇指:(
附件.instance.imageable_type.eql?(“产品”)?[“300>”,“jpg']:[“200>”,“jpg']
),
中:(
[“500>”,“jpg”]
)
}
}
结束
在哪里定义文件的保存位置?关于资产模型?或者资产模型为空?比如::storage=>:s3,:bucket=>Rails.application.config.aws_s3_bucket,:s3_credentials=>“#{Rails.root}/config/s3.yml”,:path=>”:class/:id/:style/:basename.:extension“
我只是使用默认值,并将资产模型保留为空,但我打赌有一种方法可以在资产模型中设置默认值。我没试过。你的答案真的有效吗?附件.instance.imageable_类型为nil@ArtemAminov是的,它很管用。。因为我正在我的一个项目中使用它。也许你可以帮助我完成我的项目,请看这里的代码
# controllers/images_controller.rb
def create
# params[:image][:file] ~= Image has_attached_file :file
@upload = current_user.images.build(params[:image])
# ...
end
class Image < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
has_attached_file :attachment, styles: lambda {
|attachment| {
thumb: (
attachment.instance.imageable_type.eql?("Product") ? ["300>", 'jpg'] : ["200>", 'jpg']
),
medium: (
["500>", 'jpg']
)
}
}
end