When setting up routing in Rails, I will summarize what I learned with a review.
Routing in Rails is selecting a controller and action from the requested URL.
Describe the settings in the config / routes.rb file, and the contents will be the routing settings.
config/routes.rb
Rails.application.routes.draw do
#Describe the routing here
end
The basics are described in "Method" Path "=>" Controller #Action "".
config/routes.rb
Rails.application.routes.draw do
#Example
get "about" => "top#about"
post "login" => "sessions#login
end
Can be omitted if controller # action is the same as path
config/routes.rb
Rails.application.routes.draw do
#Example
get "info/room"
end
You can name the routing with the as option. Calling "specified name_path" as a method returns a string with "/ specified name".
config/routes.rb
Rails.application.routes.draw do
#Example
get "help" => "document#help", as: "help"
end
If you write ": parameter", you can use the parameter as a path.
config/routes.rb
Rails.application.routes.draw do
#Example
get "posts/:year/:month" => "posts#show
end
To configure resource-based routing, write the resources method and pluralize the resource name. Also, to set the routing with a singular resource, describe the resource method and make the resource name singular. Rails resource base is a REST-based name for what the controller handles.
config/routes.rb
Rails.application.routes.draw do
#Example
resources :users
# get "users" => "users#index"
# get "users/:id" => "users#show"
# get "users/new" => "users#new"
# get "users/:id/edit" => "users#edit"
# post "users" => "users#create"
# patch "users/:id" => "users#update"
# delete "users/:id" => "users#destroy"
resource :account
# get "account" => "account#show"
# get "account/new" => "account#new"
# get "account/edit" => "account#edit"
# post "account" => "account#create"
# patch "account" => "account#update"
# delete "account" => "account#destroy"
end
If you want to add an additional action to the routing created by the resources method, enclose it in a block and describe the new method and action in it. Set "on :: collection" if the additional action represents a set, or "on :: member" if you want to add the id attribute.
config/routes.rb
Rails.application.routes.draw do
#Example
resources :users do
get "search", on: :collection
patch "suspend", "restore", on: :member
end
end
Of the actions created by the resources method, pass the only option or except action for unnecessary actions and set the action.
config/routes.rb
Rails.application.routes.draw do
#Example
resources :users, only: [:index, :show]
resources :users, except: [:show]
end
The path set by routing can be specified by the link_to method or redirect_to method. Example: resource name users
action | URL | path |
---|---|---|
index | users | users_path |
show | users/:id | user_path(user) |
new | users/new | new_user_path |
edit | users/:id/edit | edit_user_path(user) |
create | users | users_path |
update | users/:id | user_path(user) |
destroy | users/:id | user_path(user) |
:~view.html.erb
<%= link_to "List", users_path %>
You can get the id parameter by passing a model object as an argument. You can also use the method option to distinguish between HTTP methods.
:~view.html.erb
<%= link_to "Delete", user_path(@user), method: :delete %>
You can also specify a simplified path. If you pass a model object as the second argument, you can change it to the same path as the one with the id parameter.
:~view.html.erb
<%= link_to "Delete", users_path(@user), method: :delete %>
↓
<%= link_to "Delete", @user, method: :delete %>
Actions that handle individual resources can be set in [: Action name, model object].
:~view.html.erb
<%= link_to "Edit", edit_user_path(@user) %>
↓
<%= link_to "Edit", [edit, @user] %>
Actions that handle resources that do not require an id can be set by omitting "_path" and using a symbol.
:~view.html.erb
<%= link_to "List", users_path %>
↓
<%= link_to "List", :users %>
I gained a new understanding of routing and paths. Since I often write programs in a simplified state for routing and paths, I tend to forget the original form, so I'm glad I was able to learn how to recover now. I think that there is a way to set the routing if I examine it in detail, so I would like to add it as needed.
Recommended Posts