Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default_url in Paperclip Broke with Asset Pipeline Upgrade

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

like image 423
yellowreign Avatar asked Mar 10 '12 12:03

yellowreign


2 Answers

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

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

like image 56
JofoCodin Avatar answered Sep 19 '22 20:09

JofoCodin


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.

like image 41
tfischbach Avatar answered Sep 21 '22 20:09

tfischbach