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.
  • learn ruby on rails - ruby on rails tutorial - ruby on rails - model view controller - mvc - rails web application - ruby on rails examples
  • 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
    They determine
    • 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.
 process in ruby on rail router
Learn ruby - ruby tutorial - process in ruby on rail router - ruby examples - ruby programs
learn ruby on rails - ruby on rails tutorial - ruby on rails - rails code - model view controller - mvc - mvc routing - ruby on rails examples

Main purpose of Rails routers is explained below:

  • Connecting URLs to code.
  • Generating paths and URLs from code.
learn ruby on rails - ruby on rails tutorial - ruby on rails - rails code - rails default architecture - rails routes - ruby on rails examples

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.

learn ruby on rails - ruby on rails tutorial - ruby on rails - rails code - ruby on rails rest api - ruby on rails examples

learn ruby on rails - ruby on rails tutorial - ruby on rails - rails code - rest operation resource identifier - ruby on rails examples

learn ruby on rails - ruby on rails tutorial - ruby on rails - rails code - rest operation resource identifier - ruby on rails examples
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.
learn ruby on rails - ruby on rails tutorial - ruby on rails - rails code - ruby on rails rest api - ruby on rails examples
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:

get 'roll/:id' => 'roll#branch'  
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

This is the shorthand for,

get 'roll/:id' to: 'roll/branch'  
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

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.

Rails.application.routes.draw do   

   get 'roll/list'   
   get 'roll/new'   
   post 'roll/create'   
   patch 'roll/update'   
   get 'roll/list'   
   get 'roll/show'   
   get 'roll/edit'   
   get 'roll/delete'   
   get 'roll/update'   
   get 'roll/show_subjects'   
end   
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

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:

 rake router output in ruby on rail router
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.
learn ruby on rails - ruby on rails tutorial - ruby on rails - rails code - path helpers - ruby on rails examples

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.
learn ruby on rails - ruby on rails tutorial - ruby on rails - rails code - rest api - ruby on rails examples
learn ruby on rails - ruby on rails tutorial - ruby on rails - rails code - custom routes - rest api - ruby on rails examples

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.

This ruby on rails tutorial page provides you the following key areas such as ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby , rails form_for , ruby rails , ruby class , what is ruby on rails , rails installer , ruby online , learn ruby on rails , ruby on rails jobs , rails find_by , install rails , easyrail , rubyonrails , link_to rails , ruby on rails developer , learn ruby the hard way , railscasts , ruby on rails examples , ruby on rails vs php , rails 4 , rails activerecord , rails generate , ruby and rails , ruby on rails download , install ruby on rails , ruby net http , what is rails , ruby app , ruby vs ruby on rails , ruby on rails windows , rails for zombies , ruby on rails book , ruby on rails development , ruby on rails ide , ruby on rails tutorial pdf

Related Searches to Ruby on Rails Router