Ruby on Rails - Ruby on Rails Router - ruby on rails tutorial - rails guides - rails tutorial - ruby rails
What is Ruby on Rails Router?
- Ruby on Rails offers a fundamentally different way of dealing with URLS compared to most other languages.
- Rather than rely on the web server to control URL routing, Rails handles routing via the config/routes.rb file. This file controls every single aspect of your URLs.
- The routing module provides URL rewriting in native Ruby.
- It's a way to redirect incoming requests to controllers and actions.
- It replaces the mod_rewrite rules. http://host:port/:controller/:action/:id
- Which controller
- Which action
- Which object
- Custom routes can also be setup.
- Best of all, Rails' Routing works with any web server. Routes are defined in app/config/routes.rb.
- 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.
They determine
Learn ruby - ruby tutorial - process in ruby on rail router - ruby examples - ruby programs
Main purpose of Rails routers is explained below:
- Connecting URLs to code.
- Generating paths and URLs from code.
Restful Routes:
- 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 | What it's used for | Examples |
---|---|---|
GET | Retrieving a resource. | Any time you navigate directly to a page or use google to navigating the page, you use the GET http method. |
POST | Creating a resource | Older web applications used POST for everything. Many browsers only understand POST and GET. More on this below. |
PUT | Used to completely update a resource. | Updating your user profile on some website would typically use patch with web frameworks that support it. |
PATCH | Used to partially update a resource. | An example of this would be where you are just updating the password for your user profile. |
ruby on rails tutorial tags - ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby
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: /roll/1
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. rails new student Inside this, create a controller named as RollController . Routes will be defined for actions which are defined as methods defined in RollController class.
rails generate controller RollController
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.
1. rake routes:
Output:
Learn ruby - ruby tutorial - rake router output in ruby on rail router - ruby examples - ruby programs
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.