Home » Ruby on Rails Router

Ruby on Rails Router

The Rails router recognizes URLs and dispatches them to a controller’s action. It also generate paths and URLs. Rails router deals URLs in a different way from other language routers. It determines controller, parameters and action for the request.

Basically a router is a way to redirect incoming requests to controllers and actions. It replaces mod_rewrite rules. One best thing is that, in Rails routing works with any web server.

Rails handles routing via config/routes.rb file rather than relying on the web server to control URL routing. This file controls every single aspect of your URLs, like rules that try to match the URL path for a request and decides where to direct that request.

Main purpose of Rails routers is explained below:

  • Connecting URLs to code
  • Generating paths and URLs from code

RESTful Routes

To understand routing, we need to understand REST. Rails uses REST mainly for URL routing. So REST is important to understand Rails routing. It stands for Representational State Transfer.

There are several HTTP methods that are used with REST to represent the types of actions performed by the user or application.

HTTP Method Purpose Example
GET Retrieve a resource To navigate directly to a page or by using Google, get HTTP method is used.
POST Create a resource POST HTTP method was used for older web applications.
PUT Completely update a resource Updating user profile on a website uses patch with web frameworks that support it.
PATCH Partially update a resource It is used to only update the password for a user profile on a website.

Creating a Route

To create a route, you need to map a URL to a controller and an action. When router sees a request, it dispatches it to a controller’s action matching the URL.

If a URL looks like this:

It will be mapped to a controller’s action assuming the route is defined as:

This is the shorthand for,

The controller will be RollController and the method will be branch. The # in front of method is a way in Ruby saying that it is an instance method.

Example:

Let’s see it through an example.

Create a student application.

Inside this, create a controller named as RollController . Routes will be defined for actions which are defined as methods defined in RollController class.

Open library/config/routes.rb file and write the following code in it.

It defines actions available in the applications and type of actions such as patch, get and post.

To list defined routes in your application which are used for tracking down routing problems, use the following command.

Output:

Ruby On rails routers 1


Resource Routing

The resource routing allows you to declare all of the common routes for a controller. It defines separate routes for index, create, update, read, delete and new actions in a single line of code.

Resources on the web

Browsers request pages from a URL by certain HTTP methods like GET, POST, PUT, DELETE and PATCH. Each method performs an operation on a request.

CRUD, Verbs and Actions

A resourceful route provides a mapping between HTTP verbs and URLs to controller actions. By convention, each action maps a specific CRUD operation in a database.

Path and URL Helpers

A number of helpers to the controllers will be exposed in an application by creating a resourceful route.

Defining Multiple Resources at the same time

You can create routes for more than one resource by defining them all with a single call to resources.

Singular Resources

Singular resources are those resources which are requested by users without any referencing ID. For example, you can use singular resource to map /profile (rather than /profile/:id) to show action.

Controller Namespaces and Routing

Group of controllers are organized under a namespace. Mostly, a number of administrative controllers are named under an Admin:: namespace. These controllers are placed under the app/controllers/admin directory and can be grouped together in router.

Nested Resources

Some resources are child resources of other resources. Nested routes allow you to capture relationship in your routing.

Routing Concerns

Routing concerns allow you to declare common routes that can be reused inside other resources and routes.

Creating Paths and URLs from objects

Rails can also create paths and URLs from an array of parameters.

Adding more RESTful Actions

You are not limited to the defaults RESTful routing. You can create additional routes to apply on a collection or individual members of the collection.

Non-Resourceful Routes

Rails provide you a facility to route arbitrary URLs to actions. Here you have to set up each route within your application separately because you will not get groups of routes automatically by resourceful routing.


You may also like