Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to define a route that returns a 404

Tags:

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

like image 491
Eliot Sykes Avatar asked Jul 16 '09 18:07

Eliot Sykes


People also ask

How do you make a catch all route to handle 404 Page not found queries for ASP NET MVC?

a) add that last route to your route list. b) create a controller (in my example, i called it StaticContentController) with an Action method (in my example, i added a method called PageNotFound(..)) add logic this method to display the 404 page not found, View. If you still have the default route (I.E.

How do I create a 404 page in react router?

Every Website needs a 404 page if the URL does not exist or the URL might have been changed. To set up a 404 page in the angular routing, we have to first create a component to display whenever a 404 error occurred. In the following approach, we will create a simple react component called PagenotfoundComponent.


2 Answers

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 
like image 161
Nevir Avatar answered Oct 06 '22 20:10

Nevir


In your routes.rb:

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

In FoobarController:

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

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.

like image 37
Luke Francl Avatar answered Oct 06 '22 18:10

Luke Francl