Simplest way to define a route that returns a 404 Simplest way to define a route that returns a 404 ruby-on-rails ruby-on-rails

Simplest way to define a route that returns a 404


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


In your routes.rb:

map.my_404 '/ohnoes', :controller => 'foobar', :action => 'ohnoes'

In FoobarController:

def ohnoes  render :text => "Not found", :status => 404end

If you need to render the same 404 file as a normal 404, you can do that with render :file.

See ActionController::Base documentation for examples.


Why dont you do this in Apache/nginx where you use mod_rewrite (or however nginx does rewrites) to link to a non-existent page or instead send a 410 (Gone, no longer exists) Flag?

Anyway, if you want the rails app to do this, I think the way is as you suggested, create a named route to an action that does a render(:file => "#{RAILS_ROOT}/public/404.html", :status => 404)