[RUBY] Rails 6.0 Routing Summary

Introduction

Rails 6.0 routing path name resouces resource etc. are also summarized as a memorandum.

Explicit routing specification

routes.rb


#Routing to access the route
root 'users#show'
root to 'users#show'
root '/', to: 'users#show'

routes.rb


# '/users/:id'Route to the show action of the users controller
get '/users/:id', to: 'users#show'

routes.rb


# 'hoge'Route to show on users controller
# as:You can specify a name for the routing using the option
get '/users/:id', to: 'users#show', as: 'hoge'

When specifying the url with <% = form_with%> <% = link_to%> etc., you can directly specify it like the url such as / posts /: id, but it is not a good idea because there are many corrections when changing. .. Naming with as: makes it easier to modify and the code to read.

Difference between resources and resources

resources ... 7 actions are generated with id. Six actions are generated without id, except the resource (singular) ... index action. Resources (multiple) if there are multiple resources in the application such as "photo", "user", "product" If there is only one like "your profile" and you don't need id or index, resource (singular)

resources

routes.rb


resources :photos

With a description like this, the following 7 routes are generated. In this case, both correspond to the Photos controller.

verb path controller#action Purpose
GET /photos photos#index View a list of all photos
GET /photos/new photos#new Returns an HTML form to create one photo
POST /photos photos#create Create one photo
GET /photos/:id photos#show Show a specific photo
GET /photos/:id/edit photos#edit Returns one HTML form for photo editing
PATCH/PUT /photos/:id photos#update Update a specific photo
DELETE /photos/:id photos#destroy Delete a specific photo

resource (singular)

routes.rb


resource :geocoder

With the above description, the following 6 routes are generated. In this case, both correspond to geocoder.

verb path controller#action Purpose
GET /geocoder/new geocoders#new Returns an HTML form for geocoder creation
POST /geocoder geocoders#create Create geocoder
GET /geocoder geocoders#show Show only one geocoder resource
GET /geocoder/edit geocoders#edit Returns an HTML form for geocoder editing
PATCH/PUT /geocoder geocoders#update Update only one geocoder resource
DELETE /geocoder geocoders#destroy Delete geocoder resource

important point

Rails routing is performed from the top of the routing file. Therefore, if there are multiple routes with the same condition, only the above route will be valid. If the URL is irrelevant, the order does not matter.

reference

Rails Routing-Rails Guide v6.0 https://railsguides.jp/routing.html

Recommended Posts

Rails 6.0 Routing Summary
[Rails] devise-related routing summary
About Rails routing
[Rails] Summary of complicated routing configurations
rails db: 〇〇 Summary
Catch Rails Routing Error
[Rails] Migration command summary
[Rails] rails db command summary
rails error resolution summary
[Note] Rails3 routing confirmation
Understand Rails "shallow" routing
[Rails] Complete routing settings
Summary
Organize Rails routing using draw
Rails Tutorial (4th Edition) Summary
routing
Rails routing controller view relationship
How to write Rails routing
Ruby on Rails validation summary
Introduction to RSpec-Everyday Rails Summary-
Rails singular resource routing by resource
Ruby on Rails Overview (Beginner Summary)
Summary of rails validation (for myself)
Ruby on Rails variable, constant summary
rails tutorial chapter 10 summary (for self-learning)
Set Rails routing other than id
Ruby On Rails devise routing conflict
[Rails] AWS deployment error encounter summary
[Rails g.error]
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails Review 1
Rails API
Rails migration
Object-oriented summary
Rails Tutorial 6th Edition Learning Summary Chapter 4
[Rails] first_or_initialize
[Ruby on Rails] 1 model CRUD (Routing Main)
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
About Rails 6
[Rails Struggle/Rails Tutorial] Summary of Heroku commands
Rails / users /: id / to / {random_srting}: Dedefault Routing
Rails foundation
Rails Tutorial 6th Edition Learning Summary Chapter 5
Rails memorandum
rails tutorial
rails tutorial
rails tutorial
Rails Tutorial 6th Edition Learning Summary Chapter 2
[rails] How to configure routing in resources
ransack summary
Nesting routing ...
[Rails] devise
Rails: A little summary about data types
rails tutorial
Rails Tutorial 6th Edition Learning Summary Chapter 3
[Rails] Method summary for conversion / verification / search
rails tutorial
Rails Tips
rails method