How can I reference images in the asset pipeline from a model?

We solved this problem using draper: https://github.com/jcasimir/draper. Draper let us add a wrapper around our models (for use in views) that have access to helpers.


Personally, I don't think you should really be putting this default in a model, since it's a view detail. In your (haml) view:

= image_tag(@image.avatar_url || 'none.png')

Or, create your own helper and use it like so:

= avatar_or_default(@image)

When things like this are hard in rails, it's often a sign that it's not exactly right.


I struggled with getting this right for a while so I thought I'd post the answer here. Whilst the above works for a standard default image (i.e. same one for each paperclip style), if you need multiple default styles you need a different approach.

If you want to have the default url play nice with the asset pipeline and asset sync and want different default images per style then you need to generate the asset path without fingerprints otherwise you'll get lots of AssetNotPrecompiled errors.

Like so:

   :default_url => ActionController::Base.helpers.asset_path("/missing/:style.png", :digest => false)

or in your paperclip options:

   :default_url => lambda { |a| "#{a.instance.create_default_url}" }

and then an instance method in the model that has the paperclip attachment:

def create_default_url
   ActionController::Base.helpers.asset_path("/missing/:style.png", :digest => false)
end

In this case you can still use the interpolation (:style) but will have to turn off the asset fingerprinting/digest.

This all seems to work fine as long as you are syncing assets without the digest as well as those with the digest.