0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

resourcesの仕組みとメリット

0
Posted at

resources とは

Railsにおいて「標準的な7つのルーティング(index, new, create, show, edit, update, destroy)を一行でまとめて自動生成してくれるメソッド」のことです。

1. なぜ resources を使うのか?

Webアプリケーションでは、データの「表示・作成・編集・削除」という基本操作(CRUD:クラッド)が頻繁に行われます。これらを一つずつ get "/items/new", to: "items#new" のように書くと、コードが膨大になりミスも増えます。resources :items と書くだけで、これら一連のルートを一気に定義できます。

2. resources が作る「7つのルート」

resources :items と記述すると、以下の対応関係が自動で作られます。
スクリーンショット 0008-02-08 11.20.14.png

resources :items とすると上記の7つのアクションがすべて自動で作れます。

resources :items do
  # ...
end

あえて、Onlyで指定した場合

resources :items, only: [:index, :new, :create, :show, :edit, :update, :destroy] do
  # ...
end

only: オプション: 7つのうち、どのアクションを使いたいかを限定しています。
例 index create の二つを指定した場合以下の通り

スクリーンショット 0008-02-08 11.28.40.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?