ruby-on-rails – Rails Paperclip多态样式
发布时间:2020-12-16 21:06:18 所属栏目:百科 来源:网络整理
导读:我正在使用paperclip为使用accepts_nested_attributes_for的多个模型的附件.有没有办法为每个模型指定特定的回形针样式选项? 解决方法 是.我在站点上使用单表继承(STI)来通过Asset模型处理音频,视频和图像. # models/Asset.rbclass Asset ActiveRecord::Bas
我正在使用paperclip为使用accepts_nested_attributes_for的多个模型的附件.有没有办法为每个模型指定特定的回形针样式选项?
解决方法
是.我在站点上使用单表继承(STI)来通过Asset模型处理音频,视频和图像.
# 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 它们都进入Rails as:file,但控制器(A / V / I)知道保存到正确的模型.请记住,任何媒体形式的所有属性都需要包含在资产中:如果视频不需要字幕而图像不需要,那么视频的字幕属性将为零.它不会抱怨. 如果连接到STI模型,协会也将正常工作.用户has_many:视频的运行方式与您现在使用的相同,只是确保您不要尝试直接保存到资产. # controllers/images_controller.rb def create # params[:image][:file] ~= Image has_attached_file :file @upload = current_user.images.build(params[:image]) # ... end 最后,既然你有资产模型,你仍然可以直接从中读取,例如你想要一份20个最近资产的清单.此外,该示例不限于分离媒体类型,它也可以用于不同类型的相同事物:Avatar<资产,图库<资产等. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |