2015-09-15 21 views
4

在解决我的项目中的图片问题时遇到问题。Rails 4,Heroku上的回形针不能识别破碎的图像

总结:Rilas 4托管在Heroku使用Paerclip与S3

问题开始与具有使用以前使用的自定义上传逻辑与S3。图片网址看起来像这样/profile_picture/:style_:image_hash。它适用于那里的图像,但图像不存在的回形针仍然试图访问不存在的图像,实际的链接看起来像这样:http://s3.amazonaws.com/project/profile_pictures/100h_

has_attached_file :picture, 
       styles:   { :'53h' => '', :'100h' => '' }, 
       convert_options: { 
        :'100h' => '-gravity center -thumbnail 165x165^ -extent 165x165', 
        :'53h' => '-gravity center -thumbnail 45x45^ -extent 45x45' 
       }, 
       path:   'profile_pictures/:style_:filename', 
       default_url:  '/images/default-pp-large.jpg' 

我猜测,可能是因为实际文件名中的风格,但我不知道,eather方式defauly_url不能正常工作和图像全部被破坏,不包括实际上存在的人。

你能帮忙吗?

回答

3

最后,我做了一个猴子pach回形针宝石。添加了此行config/initializers/paperclip.rb

module Paperclip 
    class Attachment 
    alias_method :original_url, :url 

    def url(style_name = default_style, options = {}) 
     if @instance.public_send("#{@name.to_s}_file_name").blank? 
     'default-pp-large.jpg' 
     else 
     original_url(style_name, options) 
     end 
    end 
    end 
end 
1

我想知道ID如何不存在于图片的路径中,如在这种情况下,如果2个不同图片具有相同的名称,它会检索第一个匹配,我认为,路径应该是这样的:

path: 'profile_pictures/:id_:style_:filename' 
# OR 
path: 'profile_pictures/:id/:style_:filename' 

不知道是否应该完全解决问题,但这是它的一部分。

+0

我不能这样做,因为,就像我说的,迁移与现有的S3文件来了,所以改变实际路径文件是不是一种选择。 – user2945241

相关问题