Rails 5 Routing - Women Who Code.
Resources for women who code
women who code, girls who code, ladies who code
51872
post-template-default,single,single-post,postid-51872,single-format-standard,qode-core-1.0.1,ajax_fade,page_not_loaded,,capri child-child-ver-1.0.0,capri-ver-1.6, vertical_menu_with_scroll,smooth_scroll,grid_1200,blog_installed,wpb-js-composer js-comp-ver-4.12.1,vc_responsive
 

Rails 5 Routing

Rails 5 Routing

Rails Routing
Let’s learn how Rails routing works.
Routes match a browser request with the corresponding controller and action.

In routes.rb it’s typical to separate controller and action with a hash sign. For instance, here test ia a controller, and index is an action:

test#index

Simple Match Route

Simple Route, short version:

get "demo/index"

This is a routing rule that matches the demo/index url to the demo controller’s index action.

Simple route, long version (helps to understand what’s going on):

match "demo/index", :to => "demo#index", :via => :get

Again, this matches the demo/index url to the demo controller’s index action via GET request.

If you’re using only simple match routes, it means that every single page in your app would have to have a static string associated with it, so would know where to route it, and you could do it that way. But it’s not very flexible. Be better, if we can instead write a routing rule which would handle many cases that could parse the string into the right components. And that’s what the Default Route allows us to do.

Default Route

Default Route is a routing rule, that matches accordin to that rule.
Default Route, short version:

get “:controller(/:action(/:id))”

Default Route, long version:

match “:controller(/:action(/:id))”, :via => :get

Default Route formula:

:controller/:action/:id

For instance, when a GET request comes in for this URL:

/students/edit/52

We get routed to the StudentsController, edit action, id = 52.

The way we write the default route is by using a Simple Match route, but inside the string for it you would put symbols for the controller, action and id. Each one of them serves as a placeholder, it works almost like a regular expression:

Default route fell out of favor. In Rails 5, the default routing still works, but there is some indication that it will change significantly in future versions of Rails, though it’s not yet clear how. It’s still useful to see how the Default route works, but let’s not to lean on it too heavily.

Root Route

Root Route is all about the app’s homepage.

Root Route, short version:

root "demo#index"

Root Route, a bit longer version:

root to: "demo#index"

Root Route, long version:

match "/", :to => "demo#index", :via => :get

Put the root route at the top of the file, because it is the most popular route and should be matched first.

Resource Route

This one creates routes to all 7 standard controller actions. The route below maps URLs to the 7 actions of the Links controller:

resources :links

Result:

# generates:
#   get "/links"          - index action on Links controller
#   get "/links/:id"      - show action on Links controller
#   get "/links/new"      - new action on Links controller
#   post "/links"         - create action on Links controller
#   get "/links/:id/edit" - edit action on Links controller
#   put "/links/:id"      - update action on Links controller
#   patch "/links/:id"    - update action on Links controller
#   delete "/links/:id"   - destroy action on Links controller

You can specify several controllers using comma to route their 7 actions:

resources :links, :users

Resource Route with only/except:
If you want to route only some of the 7 actions, specify them with only, or skip certain actions with except.
The route below maps URLs only to the index and show actions of the Links controller:

resources :links, only [:index, :show]

Result:

# generates:
#   get "/links"          - index action on Links controller
#   get "/links/:id"      - show action on Links controller

The route below maps all URLs except index, show, new, create, update, destroy actions of the Links controller:

resources :links, only [:index, :show, :new, :create, :update, :destroy]

Result:

# generates:
#   get "/links/new" - new action on Links controller

Custom Route

# generates:
#   get "/links/new" - new action on Links controller

Reading on routing in Ruby on Rails:

  1. RailsGuides – Rails Routing from the Outside In – EN
  2. CodenaneCRUD – Маршрутизация в Rails – RU
  3. The Odin Project – Routing – EN
  4. Rusrails – Роутинг в Rails – RU
  5. mkdev – Всё, что нужно знать о routes, params и формах в Rails – RU
  6. Rails for Zombies – LEVEL 5: Routing into Darkness – EN
  7. Ruby on Rails 5 Essential Training (Lynda course by Kevin Skoglund) – video lecture 11 – EN
No Comments

Post a Comment