Simplest way to define a route that returns a 404

Eliot Sykes picture Eliot Sykes · Jul 16, 2009 · Viewed 12k times · Source

I've got a requirement to specify a named route in a Ruby on Rails project that returns the public/404.html page along with the 404 server response code.

Leaving it blank is not an option, please don't question why, it just is :) It absolutely must be a named route, or a map.connect entry would do.

Something like this would be great:

map.my_named_route '/some/route/', :response => '404'

Anyone have any idea what's the easiest way to do something like this. I could create a controller method which renders the 404.html file but thought there might be an existing cleaner way to do this. Looking forward to any responses - thanks,

Eliot

Answer

Nevir picture Nevir · Aug 25, 2012

You can route to a rack endpoint (rails 3) that vends a simple 404:

match 'my/route', to: proc { [404, {}, ['']] }

This is particularly handy, for example, to define a named route to your omniauth endpoint:

match 'auth/:action', to: proc { [404, {}, ['']] }, as: :omniauth_authorize