How to write Rails routing

Overview

When setting up routing in Rails, I will summarize what I learned with a review.

What is routing?

Routing in Rails is selecting a controller and action from the requested URL.

How to set up routing

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

How to write the routing

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

How to write resource-based routing

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

Specifying the path

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 %>

Path simplification

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 %>

Summary

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

How to write Rails routing
[Refactoring] How to write routing
How to write Rails seed
[Rails] How to write in Japanese
Rails on Tiles (how to write)
[Rails] How to write exception handling?
How to write dockerfile
How to uninstall Rails
How to write docker-compose
How to write Mockito
[rails] How to configure routing in resources
How to write migrationfile
Rails: How to write a rake task nicely
[Rails] How to write when making a subquery
[rails] How to post images
How to write good code
[Rails] How to use enum
Bit Tetris (how to write)
[Rails] How to install devise
[Rails] How to use enum
How to write java comments
How to read rails routes
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
How to use rails join
How to write Junit 5 organized
How to terminate rails server
[Rails] How to use validation
[Ruby] How to write blocks
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
[Ruby on Rails] How to write enum in Japanese
How to write a date comparison search in Rails
[Rails] How to use gem "devise"
How to deploy jQuery on Rails
[Rails] How to install Font Awesome
[Rails] How to use devise (Note)
[Rails] Two ways to write form_with
[Rails] How to use flash messages
[rails] How to display db information
Studying Java # 6 (How to write blocks)
How to use Ruby on Rails
How to deploy Bootstrap on Rails
[Rails] How to speed up docker-compose
Baseball ball count (how to write)
[Rails] How to add new pages
How to write a ternary operator
[Rails] How to install ImageMagick (RMajick)
How to write Java variable declaration
[Rails] How to install Font Awesome
[Rails] How to use Active Storage
How to introduce jQuery in Rails 6
How to return Rails API mode to Rails
How to get along with Rails
Y-shaped road tour (how to write)