Route for any image?


#1

I want to create a route for any image. This is in order to create an
email
report explaining what the missing image is. I could create a route for
each directory that currently has images in it but wanted something more
general like:

map.connect “/**/*.gif”, :controller => ‘image_handler’, :action =>
‘handle_image’

Which doesn’t work. Anyone know more about the inner workings?

Dave


#2

Here’s one way:

In your config/routes.rb file:
map.connect “*anything”, :controller => “unknown”, :action =>
“unknown_url”

In your app/controllers/unknown_controller.rb file:

Unknown URLs get routed to this action.

def unknown_url
missing_image =
@request.request_uri.match(/./(..(gif|jpg|jpeg|png))/i)

  if missing_image
     img_name = missing_image[1]
     logger.error("Request for nonexistent image #{img_name} at

URI #{@request.request_uri}…")
# Send your email here…
end

end

-Doug


#3

You can only use * once. It catches everything and stores it into a
variable of your choosing. (e.g. map.connect ‘*path’ will give you the
contents of the URL in params[:path])

So…

map.connect ‘some/*path’, :controller => ‘pages’, :action =>
‘bad_request’

But… I don’t think routes are the best way to achieve your goal, if
your goal is to scan your site for broken images. (What WOULD be a
good way I dunno…) (I doubt you can even do what you want with
routes…)

  • Rabbit