1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

routesの話

Posted at

ルーティングの色々をまとめました

###resources

resourcesはコントローラのindex、show、new、edit、create、update、destroyアクションを手軽に宣言できます。

config/routes.rb
Rails.application.routes.draw do
  resources :users
end
users     GET      /users(.:format)          users#index
          POST     /users(.:format)          users#create
new_user  GET      /users/new(.:format)      users#new
edit_user GET      /users/:id/edit(.:format) users#edit
user      GET      /users/:id(.:format)      users#show
          PATCH    /users/:id(.:format)      users#update
          PUT      /users/:id(.:format)      users#update
          DELETE   /users/:id(.:format)      users#destroy

resourcesはネストできます。
モデル同士がリレーションを持っている場合などによく使われます。
(※resourcesのネストは扱いにくくなるので1回までにしましょう)

config/routes.rb
Rails.application.routes.draw do
  resources :users do
    resources :schedules
  end
end

###member

memberブロックを渡すことで、パラメーター付きのアクションを追加できます。

config/routes.rb
Rails.application.routes.draw do
  resources :users do
    member do
      get :schedule
    end
  end
end


schedule_user  GET /users/:id/schedule(.:format)   users#schedule
users          GET      /users(.:format)           users#index
               POST     /users(.:format)           users#create
new_user       GET      /users/new(.:format)       users#new
edit_user      GET      /users/:id/edit(.:format)  users#edit
user           GET      /users/:id(.:format)       users#show
               PATCH    /users/:id(.:format)       users#update
               PUT      /users/:id(.:format)       users#update
               DELETE   /users/:id(.:format)       users#destroy

###collection
パラメーターなしのアクションはcollectionで追加できます。

config/routes.rb
Rails.application.routes.draw do
  resources :users do
    collection do
      get :schedule
    end
  end
end
schedule_users GET      /users/schedule(.:format)  users#schedule
users          GET      /users(.:format)           users#index
               POST     /users(.:format)           users#create
new_user       GET      /users/new(.:format)       users#new
edit_user      GET      /users/:id/edit(.:format)  users#edit
user           GET      /users/:id(.:format)       users#show
               PATCH    /users/:id(.:format)       users#update
               PUT      /users/:id(.:format)       users#update
               DELETE   /users/:id(.:format)       users#destroy

###namespace
namespaceは、コントローラを名前空間によってグループ化します。
controllerのディレクトリ構成は適宜変えましょう。

config/routes.rb
Rails.application.routes.draw do
  namespace :admin do
    resources :users
  end
end
admin_users     GET      /admin/users(.:format)          admin/users#index
                POST     /admin/users(.:format)          admin/users#create
new_admin_user  GET      /admin/users/new(.:format)      admin/users#new
edit_admin_user GET      /admin/users/:id/edit(.:format) admin/users#edit
admin_user      GET      /admin/users/:id(.:format)      admin/users#show
                PATCH    /admin/users/:id(.:format)      admin/users#update
                PUT      /admin/users/:id(.:format)      admin/users#update
                DELETE   /admin/users/:id(.:format)      admin/users#destroy

###module
moduleはcontrollerの格納フォルダが、指定パスになります。
URLは変えずにcontrollerのディレクトリ構成を変えたい場合はこちらを使いましょう。

config/routes.rb
Rails.application.routes.draw do
  scope module: :admin do
    resources :users
  end
end
users          GET      /users(.:format)           admin/users#index
               POST     /users(.:format)           admin/users#create
new_user       GET      /users/new(.:format)       admin/users#new
edit_user      GET      /users/:id/edit(.:format)  admin/users#edit
user           GET      /users/:id(.:format)       admin/users#show
               PATCH    /users/:id(.:format)       admin/users#update
               PUT      /users/:id(.:format)       admin/users#update
               DELETE   /users/:id(.:format)       admin/users#destroy

###scope
scopeのみの場合はURLだけが指定のパスになります。
controllerの構成はそのままです。

config/routes.rb
Rails.application.routes.draw do
  scope '/admin' do
    resources :users
  end
end
users          GET      /admin/users(.:format)           users#index
               POST     /admin/users(.:format)           users#create
new_user       GET      /admin/users/new(.:format)       users#new
edit_user      GET      /admin/users/:id/edit(.:format)  users#edit
user           GET      /admin/users/:id(.:format)       users#show
               PATCH    /admin/users/:id(.:format)       users#update
               PUT      /admin/users/:id(.:format)       users#update
               DELETE   /admin/users/:id(.:format)       users#destroy
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?