[Rails] Complete routing settings

How to define routing

Normal definition method


get '/post/:id', to: 'posts#show'

resorces Generate a resourceful set of routes. (7 actions)


resources :posts

--param option

Segment: id can be changed to any value with the param option.


resources :posts, param: :no

#=> /books/:no

--only, except option

You can limit routing to specific actions, or exclude specific actions from routing.


resources :posts, only: [:show]

resources :posts, except: [:show]

--collection, member option

collection is used for routes that do not require an id, and member uses routes that do require an id.


resources :posts, only:[] do
  collection do
    get :post_lndex , to: 'posts#post_index'
  end
  member do
    get :post_edit , to: 'posts#post_edit'
  end
end

resource

Used when using a singular resource (a resource that does not require an id reference). (Other than index action)


resource :user

root Generate application route routing.


root 'welcome#index'

HttpHelpers Helper methods of typical HTTP methods are defined.

match can use the via option to generate routes for multiple HTTP methods.


match '/posts/:id', to: 'posts#update', as: 'post', via: [:put, :patch]

#=> PUT|PATCH /posts/:id(.:format) posts#update

If the anchor option is set to false, it will be valid for all requests that match the resource starting with path.


match 'posts', to: 'posts#index', anchor: false, via: :get

scope You can create various scopes and describe routing settings.


scope '/posts' do
  get ':id', to: 'posts#show'
end

#=> GET /books/:id(.:format) posts#show

Use the controller specified by the controller option in the scope.


scope '/posts', controller: :posts do
  get '/', to: :index
  post '/', to: :create
end

#=>  GET /posts(.:format) posts#index
#=>  POST /posts(.:format) posts#create

Use the action specified by the action option within the scope.


scope '/posts/:id', controller: :posts, action: :update do
  match '/', via: [:put, :patch]
end

#=> PUT|PATCH /posts/:id(.:format) posts#update

Use the module specified by the module option in the scope.


scope '/admin', module: :admin, as: :admin do
  get '/', to: 'admin#index'
end

#=> GET /admin(.:format) admin/admin#index

The name of namespace is added to the beginning of the routing.


namespace :admin, as: 'admin' do
  # do something
end

reference

Understanding Rails Routing

Recommended Posts

[Rails] Complete routing settings
About Rails routing
Rails Routing Basics
Rails 6.0 Routing Summary
[Rails] dependent settings
Catch Rails Routing Error
[Rails] devise-related routing summary
[Note] Rails3 routing confirmation
[Rails] Custom font settings
Organize Rails routing using draw
Rails routing controller view relationship
How to write Rails routing
Rails singular resource routing by resource
[Rails] Summary of complicated routing configurations
Set Rails routing other than id
[Rails] Validation settings and Japanese localization
Ruby On Rails devise routing conflict
routing
[Ruby on Rails] 1 model CRUD (Routing Main)
Rails / users /: id / to / {random_srting}: Dedefault Routing
[rails] How to configure routing in resources
[Rails] Pre-knowledge when coding for beginners (controller creation, action creation, non-action class creation, routing settings)