The 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.
The ActionController::RoutingError
indicates that there isn't a route in the application for the URL entered by the user in the browser.
Wait, What is an ActionController?
ActionController is the "C" in the Model-View-Controller software design pattern. It is made up of several actions that are carried out in response to a request and either redirects to another action or renders a template. It serves as an intermediary between views and models, and is the foundation for all web requests made using Rails.
How to Handle an ActionController::Routing Error
When the 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 config/routes.rb
file:
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.
⚠️ If you use Heroku…
There is one common reason you might get an ActionController::RoutingError that is caused by your application and not by errant users: if you deploy your application to Heroku, or any platform that doesn't allow you to serve static files, then you might find that your CSS and JavaScript files don't load. If this is the case, the errors will look like this:
ActionController::RoutingError (No route matches [GET] "//cdn.rollbar.com/assets/application-eff78fd93759795a7be3aa21209b0bd2.css"):
To fix this and allow Rails to serve static assets you need to add a line to your application's config/environments/production.rb file:
Rails.application.configure do
# other config
config.public_file_server.enabled = true
end
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!