Rpj
Rpj

Reputation: 6110

How do I prevent unwanted routing errors in production

ActionController::RoutingError (No route matches [GET] "/google83362a7a0f381ff0.html"):

  1. I see the above logs in production, how should I prevent it.
  2. If user mistypes a URL, how should I re-direct to a common error page

Upvotes: 2

Views: 3310

Answers (3)

Gui vieira
Gui vieira

Reputation: 354

You don't need to trigger a controller to do that.

Just add this as the last rule in routes.rb:

match '*path', via: :all, to: redirect('/404')

Upvotes: 5

Suman Ranjan Panda
Suman Ranjan Panda

Reputation: 144

You can redirect the user to the desire page you want if no route matchs

Write down the following code at the bottom of your routes.rb file

In /config/routes.rb

#If no route matches
match ":url" => "application#redirect_user", :constraints => { :url => /.*/ }

Then redirect the user to the error page in the application_controller.rb file

*In /app/controllers/application_controller.rb*

def redirect_user
  redirect_to '/404'
end

Upvotes: 7

Amit
Amit

Reputation: 224

Rails does this automatically when application running in production mode.When uploading application to live server, Rails takes care of handling those exceptions and rendering the correct error pages with the correct header status.You can directly find the files inside public folder.

Whenever you set up your Rails application on a live server, you give the site root as the /public folder in your application. Then, whenever a request is made to that server address, Web server first looks in that public folder and tries to serve a static asset (this is a configurable option in config/environment.rb). If it can't find the requested page, then the request is forwarded through the Ruby stack.

When in production mode, if Rails encounters an error that isn't handled, it throws the error to the stack, which then tells Web server to render an appropriate error.

Here are some common errors that you'll see in development mode and what they render in production mode:

ActiveRecord::RecordNotFound => 404 (page not found)
nil.method => 500 (server error) unless you turn off whiny nils
ActionController::RoutingError => 404 (page not found)

Upvotes: -1

Related Questions