resourcesに慣れないので理解することを目的に個人のメモとして残します
環境
OS:Windows10
Rubyバージョン:ruby 2.5.3p105 (2018-10-18 revision 65156) [x64-mingw32]
Railsバージョン:Rails 5.2.4.1
resourcesの使い方
routes.rb
Rails.application.routes.draw do
resources :tests
end
上記のようにroutes.rbでモデルを選んで宣言
resourcesの機能
- 複数のCRUD処理に関わるルーティングを設定する(下記の表参照)
- いくつかのURLと同値である簡潔な文字列が使用できる
URLと同値な文字列 | httpメソッド | URL | コントローラー | アクション |
---|---|---|---|---|
tests | GET | /tests | tests | index |
tests | POST | /tests | tests | create |
new_test | GET | /tests/new | tests | new |
edit_test | GET | /tests/:id/edit | tests | edit |
test | GET | /tests/:id | tests | show |
test | PATCH | /tests/:id | tests | update |
test | PUT | /tests/:id | tests | update |
test | DELETE | /tests/:id | tests | destroy |
同じ文字列でもメソッドが異なるため判別されると考えられる
RESTfulについて
勉強中。随時加筆