0
我有一个使用Paperclip并将图像保存到S3的Rails应用程序。当用户上传没有图像的资源时,它将获取在回形针设置中设置的默认图像。在Rails模型中获取公共资产的URL
我的API提供这些资源,并且链接到JSON响应中的图像(使用jbuilder),但是我似乎无法返回默认图像URL,它只返回“missing.png”,我希望它将完整的URL返回给服务器并附上缺少的图像路径。
我在模型中设置了默认的URL,我尝试过使用ActionView :: Helpers :: AssetUrlHelper来获取image_url,但它永远不会工作,即使它在rails控制台内工作。任何想法我可以做什么来解决它?
的JBuilder的文件:
json.profile_picture_smallest asset.profile_picture.url(:smallest)
json.profile_picture_small asset.profile_picture.url(:small)
json.profile_picture_medium asset.profile_picture.url(:medium)
json.profile_picture_large asset.profile_picture.url(:large)
json.profile_picture_original asset.profile_picture.url(:original)
包含在模型
module Picturable
extend ActiveSupport::Concern
included do
has_attached_file :profile_picture, path: '/images/' + name.downcase.pluralize + '/:style/:basename', default_url: "missing.png",
styles: {
smallest: '50x50>',
small: '100x100>',
medium: '200x200>',
large: '400x400>',
png: ['400x400>',:png]
}, :convert_options => {
smallest: '-trim',
small: '-trim',
medium: '-trim',
large: '-trim',
png: '-trim'
}
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :profile_picture, :content_type => /\Aimage\/.*\Z/
end
def set_uuid_name
begin
self.profile_picture_file_name = SecureRandom.uuid
end while self.class.find_by(:profile_picture_file_name => self.profile_picture_file_name)
end
end
回形针配置纸夹的一部分:
Paperclip::Attachment.default_options[:s3_host_name] = 's3hostname'
开发配置:
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => 'paperclipdev',
:access_key_id => 'accesskey',
:secret_access_key => 'secretaccesskey'
}
}
请包括相关的代码:jbuilder视图,你的Paperclip配置等 – max
这样做的一种方式是上传s3中的'missing.png'并使用它的url作为图像的默认值,因为你已经在使用s3用于其他文件。 –
我更新了问题。我意识到这是一种方式,但我宁愿将它们留在轨道中,有没有办法? – brunoban