What is Routing?

Before answering that, let me ask another question: What happens when someone visits your application? To keep things simple, let's ignore the load balancers, web servers, proxy servers, etc. and just assume the request directly hits your Rails application server.

What happens when that request reaches your application?

The very first thing that needs to happen is Rails needs to figure out which part of your codebase it should send (or route) that request to, so you can return a meaningful response to the browser. And here lies the answer to the question: What is Routing?

In simple words,

Routing is the process that determines what code in your application needs to run based on the URL of the incoming request.

Router is one of the first components to run when an HTTP request hits your application. It inspects the incoming URL and directs it to the appropriate Ruby code. Typically, this code will be an action method inside a Rails controller.

Rails Router

As you can see, the router is the first point of contact for a request inside your Rails application.

After inspecting the URL, the router decides to either:

  1. forward the request to a controller action
  2. redirect the request to another URL inside your application, or even to an external website or application
  3. filter and reject the request based on pre-defined constraints or conditions, e.g. if the user is not logged in, then do not show the /admin page.

If the router can't find a route that matches the incoming request, it throws an error.

You can think of the router as a black box with a clear input and an output. Upon receiving the incoming HTTP request URL as an input, the router outputs the controller action where that request must be dispatched.

Rails Router

Now that you know what routing is, let's try to understand how routing works in the Rails framework.


If you have any questions or feedback, didn't understand something, or found a mistake, please leave a comment below or send me an email. You can also subscribe to my blog to receive future posts via email.