Man*_*ish 7 ruby-on-rails ruby-on-rails-3
我使用以下内容:
has_attached_file :file,:styles => { :thumbnail => '320x240!'},:url => "/images/:attachment/:id/:style/:basename.:extension",:path => ":rails_root/public/images/:attachment/:id/:style/:basename.:extension"
validates_attachment_content_type :file, :content_type => [ 'image/gif', 'image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg' ]
Run Code Online (Sandbox Code Playgroud)
上传图片和视频.如果我使用:style =>{}那么图像不上传.我想:style只在文件的内容类型是图像时才使用方法.
小智 6
你可以在lambda中使用condition,抱歉丑陋的格式:
has_attached_file :file, :styles => lambda
{ |a|
if a.instance.is_image?
{:thumbnail => "320x240!"}
end
}
def is_image?
return false unless asset.content_type
['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png', 'image/jpg'].include?(asset.content_type)
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1928 次 |
| 最近记录: |