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).
Solution 1:
:default_url => ActionController::Base.helpers.asset_path('missing_:style.png')
Then put the default images in app/assets/images/
Solution 2:
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.