【问题标题】:Default_url in Paperclip Broke with Asset Pipeline UpgradePaperclip 中的 Default_url 因资产管道升级而损坏
【发布时间】:2012-03-10 12:49:45
【问题描述】:

我正在使用 Paperclip,并且我的一个附件有一个这样的 default_url 选项:

:default_url => '/images/missing_:style.png'

资产管道显然不喜欢这样,因为目录移动了。处理这个问题的最佳方法是什么?这张图片有两种样式(:mini 和 :thumb)。

【问题讨论】:

  • 接受的解决方案是否仍然适合您?根据my comment below,我仍然没有找到解决方案

标签: ruby-on-rails-3 paperclip asset-pipeline


【解决方案1】:
:default_url => ActionController::Base.helpers.asset_path('missing_:style.png')

然后把默认图片放到app/assets/images/

【讨论】:

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

仅在 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

请注意,即使您的图像位于app/assets/images,您也必须为路径添加前缀images/。然后像这样使用它:

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

现在具有正确摘要哈希的默认 URL 将在生产中播放。

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

【讨论】:

  • 这对我有用。多么好的和酷的解决方案。你是怎么想出来的?我从来不知道他们实现了“插值”方法
  • 谢谢。您可以在 wiki page 上找到更多关于回形针插值的信息。
  • 多么漂亮的小解决方案。谢谢。
  • 抱歉,我对 Rails 有点陌生,不知道如何使用这种方法。我必须把插值代码放在哪里?提前致谢。
  • @halbano:您可以创建一个回形针初始化程序 (config/initializers/paperclip.rb),然后将代码粘贴到那里。
【解决方案3】:

只需确保在您的视图中您所有的回形针图像都使用image_tag 呈现。

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

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

另外,请确保您的 :default_url 与资产兼容...如果 missing_icon.png 位于 app/assets/images/missing_icon.png,则 @987654325 @ 应该只是 "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 设置不以斜杠开头。默认的default_url/:attachment/:style/missing.png,因此只需将其更改为:attachment/:style/missing.png 将导致一切都开始在生产中工作(使用预编译资产)并继续在开发中工作......
【解决方案4】:

我在 assets:precompile with 处收到错误(即使是单一样式)

: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

我没有尝试多种样式,但这适合我的情况。

【讨论】:

  • 适用于 rails 4.1
  • 或全部在 lambda 中:default_url: -&gt; (a) { ActionController::Base.helpers.asset_path('missing.jpg') }
【解决方案5】:

这对我有用:

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

【讨论】:

  • 谢谢!没有其他人解释说 :style 只是从前面的描述中获取!!!!
【解决方案6】:

在 rails 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' }

【讨论】:

    【解决方案7】:

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

    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
    

    我静态编译资产,但在生产中遇到错误,这对我有帮助。

    【讨论】:

      【解决方案8】:

      在您的模型文件中,更改此行:

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

      删除这个:

      /images/
      

      在assests/images中为每种样式创建一个文件夹,在本例中为medium和thumb,并在其中放置一个名为missing.png的图像(当然,只要它与文件名匹配,也可以使用任何名称在模型中)

      为我工作。

      【讨论】:

        【解决方案9】:

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

        建议使用其他解决方案的问题

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

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

        我使用以下代码创建了一个初始化程序:

        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, ...
        

        【讨论】:

          【解决方案10】:

          只需从/images/pic.png 中删除/images/pic.png

          【讨论】:

            猜你喜欢
            • 2018-09-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-08-17
            • 1970-01-01
            • 1970-01-01
            • 2010-10-28
            相关资源
            最近更新 更多