Forum: Ruby on Rails Route for any image?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bd6167863690cb320d65bb9204619817?d=identicon&s=25 David Clements (Guest)
on 2006-03-15 21:12
(Received via mailing list)
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
42292bf8a0acbc98862d9306b501275d?d=identicon&s=25 Daniel Waite (rabbitcreative)
on 2006-03-15 21:30
(Received via mailing list)
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

---
6567d3f35d258037368e834fa9f9813b?d=identicon&s=25 Doug Fales (Guest)
on 2006-03-15 22:10
(Received via mailing list)
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
This topic is locked and can not be replied to.