Paperclip Broke中的Default_url与资产管道升级

yel*_*ign 33 paperclip ruby-on-rails-3 asset-pipeline

我正在使用Paperclip并为我的一个附件设置了一个default_url选项:

:default_url => '/images/missing_:style.png'
Run Code Online (Sandbox Code Playgroud)

自从目录移动后,资产管道显然不喜欢这样.处理这个问题的最佳方法是什么?我对这张照片有两种风格(迷你和拇指).

Jof*_*din 45

:default_url => ActionController::Base.helpers.asset_path('missing_:style.png')
Run Code Online (Sandbox Code Playgroud)

然后将默认图像放在app/assets/images /

  • 在我的开发或生产(Heroku)环境中,此解决方案在Rails 4.1中不起作用.在这两种环境中,我的图像都在"app/assets/images"中,当我打开一个Rails控制台并尝试`ActionController :: Base.helpers.asset_path('missing.png')`时,它打印出正确的指纹路径(`/资产/缺longmd5hash.png`).但是,`Model.attachment.url`为没有附件的Model实例返回的默认URL只返回`/ missing.png`(末尾没有`/ assets`前缀或指纹).什么想法可能会出错? (9认同)
  • 如果此解决方案不起作用,请参阅我对github的评论https://github.com/thoughtbot/paperclip/issues/1597#issuecomment-123496278.简而言之:只需在lambda中包装#asset_path即可. (9认同)
  • 这不适用于生产env,引发错误:attachments::class /:attachment /:style/missing.jpg不是预编译的actionpack(3.1.4)lib/sprockets/helpers/rails_helper.rb:147:in`digest_for " (6认同)
  • 默认情况下,@ JofoCodin Rails在生产中运行时不编译资产(速度很慢).您需要在推送到生产之前预编译资产:`bundle exec rake assets:precompile` (3认同)

tfi*_*ach 24

仅在Rails 4上测试过.

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

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

请注意,即使您的图像位于路径中,也不得为路径添加前缀.然后使用它像:images/app/assets/images

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

现在,在生产中播放具有正确摘要哈希的默认网址.

default_url选项也采用lambda,但我找不到确定请求样式的方法,因为插值仅应用于lambda的结果.


Bri*_*pel 15

只需确保在视图中所有的回形针图像都使用image_tag.

<%= image_tag my_model.attachment.url(:icon) %>
Run Code Online (Sandbox Code Playgroud)

这样,:crazy :symbol :interpolation在Rails尝试将其解析为管道中的资产之前,所有回形针都会发生在url字符串中.

此外,请确保您: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">
Run Code Online (Sandbox Code Playgroud)

  • 基本上,您需要做的就是确保您的`default_url`设置不以斜杠开头.默认的`default_url`是`/:attachment /:style/missing.png`,所以只需将其更改为`:attachment /:style/missing.png`将导致所有内容开始在生产中工作(使用预编译资产)并继续在开发中工作... (6认同)

小智 8

我在资产上得到了错误(即使是单一样式):precompile with

:default_url => ActionController::Base.helpers.asset_path('missing.png')
Run Code Online (Sandbox Code Playgroud)

所以我迷上了这样的方法

# 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
Run Code Online (Sandbox Code Playgroud)

我没有尝试多种样式,但这适用于我的情况.