Default_url in Paperclip Broke with Asset Pipeline Upgrade

大兔子大兔子 提交于 2019-11-27 07:25:35
:default_url => ActionController::Base.helpers.asset_path('missing_:style.png')

Then put the default images in app/assets/images/

Tested only on Rails 4.

To make it work in production, we have to pass the name of an existing file to the asset_path helper. Passing a string containing a placeholder like "missing_:style.png" therefore doesn't work. I used a custom interpolation as a workaround:

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

Note that you must not prefix the path with images/ even if your image is located in app/assets/images. Then use it like:

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

Now default urls with correct digest hashes are played out in production.

The default_url option also takes a lambda, but I could not find a way to determine the requested style since interpolations are only applied to the result of the lambda.

Just make sure that in your views all your paperclip images are rendered with image_tag.

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

That way, all of paperclip's :crazy :symbol :interpolation will have happened to the url string before Rails tries to resolve it to an asset in the pipeline.

Also, make sure your :default_url is asset compatible...if missing_icon.png is at app/assets/images/missing_icon.png, then :default_url should be simply "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">

I got the error(even for a single style) at assets:precompile with

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

So I hooked with a method like this

# 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

I didn't try for multiple styles, but this works for my situation.

this works for me:

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

just place images in your assets/images folder named: missing_large.png, missing_medium.png, missing_small.png and missing_thumb.png

In rails 4.0.0 and paperclip 4.1.1 this worked for me:

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

I ended up having to use something like the following.

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

I statically compile the assets and was getting an error in production, this helped me.

In your model file, change this line:

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

by removing this:

/images/

Create a folder for each style, in this example medium and thumb, in assests/images and place an image called missing.png there (or whatever name you want it to have of course, as long as it matches the file name in the model)

Worked for me.

I've solved this problem by using a custom interpolator.

The problem from other solutions that suggest using

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

is that you will get an error saying "missing_style.png" is not precompiled.

I created an initializer with the following code:

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

Then in my model I would do:

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

Just remove the / from /images/pic.png: images/pic.png

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!