Forum: Ruby on Rails Best way to create a route that only accepts one format

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.
Joshua A. (Guest)
on 2008-12-27 05:34
Hey guys,

I haven't done this before so I thought I'd ping the mailing list to see
if anyone has any better ideas than what I've come up with.

I want to create a route that looks something like this:

url.com/counter/:id.png

Basically, this will route to the "show" method in the controller, and
should only work if the format requested is .png.

I'm using this controller for other things as well, but any other time
it's accessed through a nested route:

url.com/users/1/ads/:id

So here's what I've come up with:

  map.resources :ads, :only => :show, :as => :counter, :requirements =>
{ :format => :png }
  map.resources :users, :has_many => :ads

This seems to be working, however instead of a 404 Not Found being
returned to the browser, it's returning a 406 Not Acceptable.

Is there any way to generate this route so that unless the format is png
it will return the usual 404 Not Found when a route doesn't exist?

Thanks,
Josh
This topic is locked and can not be replied to.