11 9 / 2011

Many things can cause your production asset paths to change. When that happens, your previous assets just stop working at all. Moreover, it seems like it would make sense for the bare asset path (without the md5 postfix) should just point to the most recent version of the asset. This little tidbit should take care of all of that.

Myapp::Application.routes.draw do
  match 'assets/:id.:format' => 'assets#show'
end

class AssetsController < ApplicationController
  def show
    asset_without_digest = "#{params[:id].gsub /-[0-9a-f]{32}$/, ''}.#{params[:format]}"
    new_path = ActionView::Base.new.image_path(asset_without_digest)
    if url_for(params).include? new_path
      head 404
    else
      redirect_to new_path, :status => 301
    end
  end
end