Routes ::: Very Important Themes about Route

 Routes ::: Very Important Themes about Route


........................................................................................................................................................                                                                                                                                            ....




........................................................................................................................................................                                                                                                                                            ....

01:   Defining Multiple Resources at the Same Time


resources :photos, :books, :videos

........................................................................................................................................................                                                                                                                                            ....

........................................................................................................................................................                                                                                                                                            ....

2   Path and URL Helpers

Creating a resourceful route will also expose a number of helpers to the controllers in your application. In the case of resources :photos:

  • photos_path returns /photos
  • new_photo_path returns /photos/new
  • edit_photo_path(:id) returns /photos/:id/edit (for instance, edit_photo_path(10) returns /photos/10/edit)
  • photo_path(:id) returns /photos/:id (for instance, photo_path(10) returns /photos/10)

Each of these helpers has a corresponding _url helper (such as photos_url) which returns the same path prefixed with the current host, port, and path prefix.

........................................................................................................................................................                                                                                                                                            ....

........................................................................................................................................................                                                                                                                                            ....


You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an Admin:: namespace. You would place these controllers under the app/controllers/admin directory, and you can group them together in your router:


namespace :admin do
  resources :articles, :comments
end

This will create a number of routes for each of the articles and comments controller. For Admin::ArticlesController, Rails will create:

HTTP VerbPathController#ActionNamed Route Helper
GET/admin/articlesadmin/articles#indexadmin_articles_path
GET/admin/articles/newadmin/articles#newnew_admin_article_path
POST/admin/articlesadmin/articles#createadmin_articles_path
GET/admin/articles/:idadmin/articles#showadmin_article_path(:id)
GET/admin/articles/:id/editadmin/articles#editedit_admin_article_path(:id)
PATCH/PUT/admin/articles/:idadmin/articles#updateadmin_article_path(:id)
DELETE/admin/articles/:idadmin/articles#destroyadmin_article_path(:id)


If you want to route /articles (without the prefix /admin) to Admin::ArticlesController, you could use:


scope module: 'admin' do
  resources :articles, :comments
end

or, for a single case:

resources :articles, module: 'admin'

If you want to route /admin/articles to ArticlesController (without the Admin:: module prefix), you could use:

scope '/admin' do
  resources :articles, :comments
end

or, for a single case:

resources :articles, path: '/admin/articles'

In each of these cases, the named routes remain the same as if you did not use scope. In the last case, the following paths map to ArticlesController:

HTTP VerbPathController#ActionNamed Route Helper
GET/admin/articlesarticles#indexarticles_path
GET/admin/articles/newarticles#newnew_article_path
POST/admin/articlesarticles#createarticles_path
GET/admin/articles/:idarticles#showarticle_path(:id)
GET/admin/articles/:id/editarticles#editedit_article_path(:id)
PATCH/PUT/admin/articles/:idarticles#updatearticle_path(:id)
DELETE/admin/articles/:idarticles#destroyarticle_path(:id)

If you need to use a different controller namespace inside a namespace block you can specify an absolute controller path, e.g: get '/foo', to: '/foo#index'.


........................................................................................................................................................                                                                                                                                            ....

........................................................................................................................................................                                                                                                                                            ....


4  Nested Resources



It's common to have resources that are logically children of other resources. For example, suppose your application includes these models:

class Magazine < ApplicationRecord
  has_many :ads
end
 
class Ad < ApplicationRecord
  belongs_to :magazine
end

Nested routes allow you to capture this relationship in your routing. In this case, you could include this route declaration:

resources :magazines do
  resources :ads
end

In addition to the routes for magazines, this declaration will also route ads to an AdsController. The ad URLs require a magazine:



HTTP VerbPathController#ActionUsed for
GET/magazines/:magazine_id/adsads#indexdisplay a list of all ads for a specific magazine
GET/magazines/:magazine_id/ads/newads#newreturn an HTML form for creating a new ad belonging to a specific magazine
POST/magazines/:magazine_id/adsads#createcreate a new ad belonging to a specific magazine
GET/magazines/:magazine_id/ads/:idads#showdisplay a specific ad belonging to a specific magazine
GET/magazines/:magazine_id/ads/:id/editads#editreturn an HTML form for editing an ad belonging to a specific magazine
PATCH/PUT/magazines/:magazine_id/ads/:idads#updateupdate a specific ad belonging to a specific magazine
DELETE/magazines/:magazine_id/ads/:idads#destroydelete a specific ad belonging to a specific magazine

This will also create routing helpers such as magazine_ads_url and edit_magazine_ad_path. These helpers take an instance of Magazine as the first parameter (magazine_ads_url(@magazine)).



........................................................................................................................................................                                                                                                                                            ....

........................................................................................................................................................                                                                                                                                            ....


5 Adding More RESTful Actions

You are not limited to the seven routes that RESTful routing creates by default. If you like, you may add additional routes that apply to the collection or individual members of the collection.


2.10.1 Adding Member Routes

To add a member route, just add a member block into the resource block:

resources :photos do
  member do
    get 'preview'
  end
end

This will recognize /photos/1/preview with GET, and route to the preview action of PhotosController, with the resource id value passed in params[:id]. It will also create the preview_photo_url and preview_photo_path helpers.

Within the block of member routes, each route name specifies the HTTP verb that will be recognized. You can use getpatchputpost, or delete here . If you don't have multiple member routes, you can also pass :on to a route, eliminating the block:

resources :photos do
  get 'preview', on: :member
end

You can leave out the :on option, this will create the same member route except that the resource id value will be available in params[:photo_id] instead of params[:id]. Route helpers will also be renamed from preview_photo_url and preview_photo_path to photo_preview_url and photo_preview_path.

2.10.2 Adding Collection Routes

To add a route to the collection:

resources :photos do
  collection do
    get 'search'
  end
end

This will enable Rails to recognize paths such as /photos/search with GET, and route to the search action of PhotosController. It will also create the search_photos_url and search_photos_path route helpers.

Just as with member routes, you can pass :on to a route:

resources :photos do
  get 'search', on: :collection
end

If you're defining additional resource routes with a symbol as the first positional argument, be mindful that it is not equivalent to using a string. Symbols infer controller actions while strings infer paths.



2.10.3 Adding Routes for Additional New Actions

To add an alternate new action using the :on shortcut:

resources :comments do
  get 'preview', on: :new
end

This will enable Rails to recognize paths such as /comments/new/preview with GET, and route to the preview action of CommentsController. It will also create the preview_new_comment_url and preview_new_comment_path route helpers.

If you find yourself adding many extra actions to a resourceful route, it's time to stop and ask yourself whether you're disguising the presence of another resource.



........................................................................................................................................................                                                                                                                                            ....

........................................................................................................................................................                                                                                                                                            ....








Comments

Popular posts from this blog

Rails 7 Features :: Comparison with Rails 6 and Rails 5