We've summarized routing in Rails applications.
Check the "effect of routing" and "practical usage".
** Conclusion ** -It will be possible to pass processing to the controller and action based on the HTTP request. -Helper methods allow you to create paths and URIs in controllers and views.
In routing, HTTP request from client is distributed by the following code.
Routing image
[HTTP method] '[URI path]', to: '[Controller name]#[Action name]'
Example) get 'samples', to: 'samples#index'
In other words, in this example ...
When the client accesses with ** "GET method" ** and ** "samples path" ** Process with ** "index action" ** of ** "samples controller" ** of Rails application
It means that · · ·
By describing the routing, you can use ** "\ _path method" and "\ _url method" ** in Rails application. This method allows you to write redirect_to methods in controllers, link_to methods in views, etc. using path.
It is a helper method that easily generates paths and URLs by using it like ** "[Prefix] \ _ path" ** or ** "[Prefix] \ _ url" **.
It will be the config / routes.rb
file.
config/routes.rb
Rails.application.routes.draw do
get 'samples', to: 'samples#index'
end
You can check it by running the command rails routes
in the application directory using the terminal.
Terminal
(Application directory)$ rails routes
Prefix Verb URI Pattern Controller#Action
samples GET /samples(.:format) samples#index
** "Prefix", "Verb", "URI Pattern", "Controller # Action" ** are displayed. Determine the HTTP request content by combining ** "Verb and URI Pattern" ** ** "Controller # Action" ** indicates where to pass the process.
It is like a variable used when using the "\ _path method" and "\ _url method".
Example) samples_path => "/ samples" samples_url => " http://sample.com/samples "
It is an HTTP method that determines the processing.
This is the path of the URI to be processed.
It will be the controller name and action name to pass the process.
Rails has ** "7 types of actions" ** that are used as standard.
Action name | Intended use |
---|---|
index | List |
new | Show new data form |
create | Register new data |
show | Display specific data |
edit | Display the edit form of specific data |
update | Update specific data |
destroy | Delete specific data |
It can be roughly divided into two. ** "Individual settings" ** and ** "Batch settings" **.
This is the code that sets the routing ** line by line **.
Rails.application.routes.draw do
get 'samples', to: 'samples#index'
post 'samples', to: 'samples#create'
put 'samples/:id', to: 'samples#update'
end
You can write multiple routing settings in short code.
It is a code that can set the routing of ** 7 actions ** in one line.
Rails.application.routes.draw do
resources :samples
end
Same as the individual configuration routing below
Rails.application.routes.draw do
get 'samples', to: 'samples#index'
post 'samples', to: 'samples#create'
get 'samples/new', to: 'samples#new', as: 'new_sample'
get 'samples/:id/edit', to: 'samples#edit', as: 'edit_sample'
get 'samples/:id', to: 'samples#show', as: 'sample'
patch 'samples/:id', to: 'samples#update'
put 'samples/:id', to: 'samples#update'
delete 'samples/:id', to: 'samples#destroy'
end
By setting ** "as:'XXX'" **, you can specify "XXX" ** in ** Prefix.
It is a code that can set the routing of 6 actions ** other than the ** index action in one line. It is used when ** each user has only one piece of information (resource) in the application design. Since it has only one piece of information, there is no index action (listing multiple resources) ** ": id" ** is not added to the URI path.
Also note that ** controllers are plural **.
Rails.application.routes.draw do
resource :sample #It is singular
end
Same as the individual configuration routing below
Rails.application.routes.draw do
post 'sample', to: 'samples#create'
get 'sample/new', to: 'samples#new', as: 'new_sample'
get 'sample/edit', to: 'samples#edit', as: 'edit_sample'
get 'sample', to: 'samples#show', as: 'sample'
patch 'sample', to: 'samples#update'
put 'sample', to: 'samples#update'
delete 'sample', to: 'samples#destroy'
end
When you want to use some of the resources / resource routing actions ** Specify "only option" ** and ** "except option" **.
Rails.application.routes.draw do
#Use only index / show actions
resources :users, only: [:index, :show]
#Use other than index / show / edit / update / destroy actions
resources :books, except: [:index, :show, :edit, :update , :destroy]
end
$ rails routes
Prefix Verb URI Pattern Controller#Action
users GET /users(.:format) users#index
user GET /users/:id(.:format) users#show
books POST /books(.:format) books#create
new_book GET /books/new(.:format) books#new
In addition to the seven basic Rails actions, you can add actions with ** "member routing" ** and ** "collection routing" **.
This is a routing method that includes ** ": id" ** in the URI path.
member routing
Rails.application.routes.draw do
resources :photos do
member do
get 'preview'
end
end
end
$ rails routes
Prefix Verb URI Pattern Controller#Action
preview_photo GET /photos/:id/preview(.:format) photos#preview
photos GET /photos(.:format) photos#index
A routing method that does not include the URI path ** ": id" **. The code is written in the same way as member routing.
"Id" is the management number of the data registered in the application. The fact that there is an "id" in the path means that "when performing that action, the required data is specified by the control number".
For example, if you explain with 7 basic actions of Rails The ** index / new / create actions ** do not have an "id" in the path. The reason is that ** no specific data ** is needed to display the list, and ** the data to be registered ** does not have an "id".
On the contrary, ** show / edit / update / destroy actions ** ** Because you can view, change, or delete specific registered data ** Without the "id" ** I don't know what data **, so the path contains the "id".
It means "nested". Indicates that the child element is nested inside the parent element in the routing.
Example)Routing of nested resources
Rails.application.routes.draw do
resources :books do
resources :reviews
end
end
In this example, the parent element called books has a nested structure with the child element called reviews.
Nested routing allows you to associate a parent element with the routing of a child element. In the case of the above example, reviews (child elements) are always tied to a specific book (parent element). If you check the routing of reviews (child element), it will look like this.
Prefix Verb URI Pattern Controller#Action
book_reviews GET /books/:book_id/reviews(.:format) reviews#index
POST /books/:book_id/reviews(.:format) reviews#create
new_book_review GET /books/:book_id/reviews/new(.:format) reviews#new
edit_book_review GET /books/:book_id/reviews/:id/edit(.:format) reviews#edit
book_review GET /books/:book_id/reviews/:id(.:format) reviews#show
PATCH /books/:book_id/reviews/:id(.:format) reviews#update
PUT /books/:book_id/reviews/:id(.:format) reviews#update
DELETE /books/:book_id/reviews/:id(.:format) reviews#destroy
The ** "Prefix" ** and ** "URI Pattern" ** items have a nesting effect.
The parent element comes before the child element.
It is a Prefix that contains parent elements (books) such as book_reviews
and new_book_review
.
The parent element and the id of the parent element are placed before the child element.
It will be a URI Pattern containing the parent element (book) / books /: book_id / reviews
.
The / books /: book_id
part is the path that represents a specific parent element
"Which parent element the child element is connected to" will be explicitly shown.
That's it.
Recommended Posts