ActionController::RoutingError is the most common error faced when working on a Ruby on Rails project - it’s equivalent to the classic 404 error in web applications.
ActionController::RoutingError indicates that there isn't a route in the application for the URL entered by the user in the browser.
How to Handle an ActionController::Routing Error
ActionController::RoutingError error occurs, a log record is created as follows:
ActionController::RoutingError (No route matches [GET] "....."):
It’s best to explicitly catch and throw the
ActionController::RoutingError with a custom message. This is because they may go unnoticed in the log files.
To do so, add the following code in your
Rails.application.routes.draw do # all your other routes match '*unmatched', to: 'application#not_found_method', via: :all end
Now you need to add this
not_found_method in your ApplicationController.
class ApplicationController < ActionController::Base protect_from_forgery with::exception def not_found_method render file: Rails.public_path.join('404.html'), status: :not_found, layout: false end end
This will serve your 404 page every time a user-provided URL lacks a suitable route. Keep in mind though that any route or engine that is mounted after the application loads won’t be reachable as they will be caught by the catch all route.
Though it’s not easy to resolve the error, one solution is to run the below command in the terminal.
$ rails routes
This will give you all the routes that have been created here, and you can check if a route is not present. You can then go back to
config/routes.rb and define the missing route.
Track, Analyze and Manage Errors With Rollbar
Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Ruby on Rails errors easier than ever. Try it today!