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 /photosnew_photo_path returns /photos/newedit_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:
This will create a number of routes for each of the articles and comments controller. For Admin::ArticlesController, Rails will create:
HTTP Verb Path Controller#Action Named Route Helper GET /admin/articles admin/articles#index admin_articles_path GET /admin/articles/new admin/articles#new new_admin_article_path POST /admin/articles admin/articles#create admin_articles_path GET /admin/articles/:id admin/articles#show admin_article_path(:id) GET /admin/articles/:id/edit admin/articles#edit edit_admin_article_path(:id) PATCH/PUT /admin/articles/:id admin/articles#update admin_article_path(:id) DELETE /admin/articles/:id admin/articles#destroy admin_article_path(:id)
If you want to route /articles (without the prefix /admin) to Admin::ArticlesController, you could use:
or, for a single case:
If you want to route /admin/articles to ArticlesController (without the Admin:: module prefix), you could use:
or, for a single case:
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 Verb Path Controller#Action Named Route Helper GET /admin/articles articles#index articles_path GET /admin/articles/new articles#new new_article_path POST /admin/articles articles#create articles_path GET /admin/articles/:id articles#show article_path(:id) GET /admin/articles/:id/edit articles#edit edit_article_path(:id) PATCH/PUT /admin/articles/:id articles#update article_path(:id) DELETE /admin/articles/:id articles#destroy article_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:
Nested routes allow you to capture this relationship in your routing. In this case, you could include this route declaration:
In addition to the routes for magazines, this declaration will also route ads to an AdsController. The ad URLs require a magazine:
HTTP Verb Path Controller#Action Used for GET /magazines/:magazine_id/ads ads#index display a list of all ads for a specific magazine GET /magazines/:magazine_id/ads/new ads#new return an HTML form for creating a new ad belonging to a specific magazine POST /magazines/:magazine_id/ads ads#create create a new ad belonging to a specific magazine GET /magazines/:magazine_id/ads/:id ads#show display a specific ad belonging to a specific magazine GET /magazines/:magazine_id/ads/:id/edit ads#edit return an HTML form for editing an ad belonging to a specific magazine PATCH/PUT /magazines/:magazine_id/ads/:id ads#update update a specific ad belonging to a specific magazine DELETE /magazines/:magazine_id/ads/:id ads#destroy delete 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:
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 get, patch, put, post, or delete here . If you don't have multiple member routes, you can also pass :on to a route, eliminating the block:
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:
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:
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:
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
Post a Comment