Default_url en Paperclip se rompió con la actualización de la canalización de activos

I'm using Paperclip and have a default_url option like this for one of my attachments:

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

The asset pipeline obviously doesn't like this since the directories moved. What's the best way to handle this? I have two styles for this picture (:mini and :thumb).

preguntado el 10 de marzo de 12 a las 12:03

does the accepted solution still work for you? As per mi comentario a continuación, I still haven't found a solution for this -

10 Respuestas

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

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

respondido 19 mar '12, 18:03

this not works in production env, following error raised: attachments/:class/:attachment/:style/missing.jpg isn't precompiled actionpack (3.1.4) lib/sprockets/helpers/rails_helper.rb:147:in `digest_for' - lidobing

@lidaobing and what do you do in production? - Matías

@JofoCodin Rails, by default, doesn't compile assets when running in production (it's slow). You need to precompile assets before pushing to production: bundle exec rake assets:precompile - jamones

This solution does not work for me in Rails 4.1 in either my development or production (Heroku) environments. In both environments, my images are in app/assets/images, and when I open a Rails console and try ActionController::Base.helpers.asset_path('missing.png'), it prints out the properly fingerprinted path (/assets/missing-longmd5hash.png). However, the default URLs returned by Model.attachment.url for instances of Model without an attachment only return /missing.png (doesn't have the /assets prefix or the fingerprint at the end). Any ideas what might be going wrong? - hunteros

If this solution doesn't work see my comment on github… . In short: just wrap #asset_path in lambda. - Icem

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|

Tenga en cuenta que debe no prefijo la ruta con images/ even if your image is located in app/assets/images. Then use it like:

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

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

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

respondido 05 nov., 14:11

This works for me. What a nice and cool solution. How did you figure that out? I never knew they implement "interpolates" method - Yudho Ahmad Diponegoro

Thanks. You can find out a little bit more about Paperclip interpolations on this página wiki. - fischbach

What a beautiful little solution. Thanks. - bswinnerton

Sorry, but I'm a bit new in rails and I don't know how to use this approach. Where I have to put the interpolation code? Thanks in advance. - habano

@halbano: You can create a paperclip initializer (config/initializers/paperclip.rb) and simply paste the code in there. - fischbach

Solo asegúrate de que en tus puntos de vista 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.

Además, asegúrese de que su :default_url is asset compatible...if missing_icon.png Está en app/assets/images/missing_icon.png, entonces :default_url debería ser simplemente "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">

Respondido el 19 de junio de 12 a las 19:06

Essentially, all you need to do is ensure your default_url setting doesn't start with a slash. The default default_url is /:attachment/:style/missing.png, so just changing that to :attachment/:style/missing.png will cause everything to start working in production (with precompiled assets) and continue working in dev... - fidothe

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

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

Respondido el 07 de Septiembre de 12 a las 05:09

Worked properly for rails 4.1 - rodrigo dias

Or all in the lambda: default_url: -> (a) { ActionController::Base.helpers.asset_path('missing.jpg') } - Mar

esto funciona para mi:

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

respondido 08 mar '14, 23:03

Thank you! no one else explained that the :style would just take it from the previous description!!!! - Arturo Collé

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

Respondido 16 Abr '14, 09:04

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.

respondido 19 nov., 14:16

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:


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)

Trabajó para mi.

Respondido 06 Oct 16, 12:10

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)

Then in my model I would do:

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

Respondido el 12 de enero de 14 a las 19:01

Solo quita el / desde /images/pic.png: images/pic.png

Respondido el 08 de diciembre de 14 a las 12:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.