Rails 3.1 asset pipeline: how to deal with requests to expired assets?
Once you've set up an application with the asset pipeline, it might generate an image for you with a URL like this (signature truncated):
http://mysite.com/assets/logo-1b2b3c.png
Then, when you change the asset or update the global asset version, you end up with something like this:
http://mysite.com/assets/logo-2b3c1a.png
Now, some people will still be sending requests to the old asset. Under what I understand to be the default Rails 3.1 deploy, these would just 404. That doesn't seem very friendly. It would be nicer to either 301 to 开发者_StackOverflow中文版the new asset or just deliver the new asset straight up.
Is there some built-in way to do this, or do I have to implement my own asset missing feature?
I implemented it myself:
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
精彩评论