Joe Essey
Joe Essey

Reputation: 3537

How can I route to a page any time no route is available?

I want to make a simple page with a back button for the odd case that a user enters a url to a page there is not route for.

For instance, the route for foo is:

resources :foos, :except => [:index]

The user enters:

mysite.com/foos

I want to display a page that says "This page doesn't exist" and a back button.

Where do I put the html.erb file and how to I account for that in routes.rb?

Thanks

Upvotes: 2

Views: 83

Answers (4)

Zippie
Zippie

Reputation: 6088

At the end of your routes.rb write:

match '*path', :controller => 'some_controller', :action => 'some_action'

or

match '*path' => 'some_controller#some_action'

Source:

rails handle 404 with url redirect

Upvotes: 2

Kiran
Kiran

Reputation: 20313

As per my knowledge you have two ways to do this:

  1. If you would like to capture errors like (404,500..etc.,) use rescue_from in ActionController. Otherwise if you just want to edit the default error pages, edit the 500.html and 404.html files in {Rails.root}/public

Example: How to properly render custom 404 and 500 pages?

2.Custom Error Page - Ruby on Rails

Upvotes: 1

amb110395
amb110395

Reputation: 1545

When a client's request can not be found in the server, a 404 redirect is made. To costumize the 404 page, simply change it in public/404.html

Upvotes: 0

Mindbreaker
Mindbreaker

Reputation: 1025

In the production mode, the 404.html in the /public folder of your Rails Application will be renderd for a Routing Error instead of displaying the error message.

Upvotes: 1

Related Questions