LoginSignup
2
2

More than 1 year has passed since last update.

resourceとresourcesの違いについて

Posted at

はじめに

本記事では、resourceとresourcesの違いについて記述します。

resources

resourcesとは、
7つのアクションへのルーティングを自動生成するメソッドです。

resourcesの引数に、:itemsなどという+シンボルで指定すると/usersのパスに対応するルーティングが生成されます。

% rails routes

GET          /items(.:format)                   items#index
POST         /items(.:format)                   items#create
GET          /items/new(.:format)               items#new
GET          /items/:id/edit(.:format)          items#edit
GET          /items/:id(.:format)               items#show
PATCH        /items/:id(.:format)               items#update
PUT          /items/:id(.:format)               items#update
DELETE       /items/:id(.:format)               items#destroy

resource

resourceとは、
7つのアクションへのルーティングを自動生成するメソッドですが、
indexidが付いているパスが生成されません。

% rails routes

POST         /items(.:format)                   items#create
GET          /items/new(.:format)               items#new
GET          /items/edit(.:format)              items#edit
GET          /items(.:format)                   items#show
PATCH        /items(.:format)                   items#update
PUT          /items(.:format)                   items#update
DELETE       /items(.:format)                   items#destroy

使い分け

「user」や「item」など、そのアプリケーションに複数存在ようなものについては、
「id」は存在し、resourcesを使う必要があると思います。

しかし、そのアプリケーション上に1つしか存在しないものについては、
「id」は必要ないため、resourceでも良いと思います。

終わりに

簡単ですが、過去にひっかかってしまったところでしたので、記事にしました。
知ってそうで知らないものはまだまだありそうです...。

以下参考記事です。
resourcesとresourceの違いについて!
resourceとresourcesの違い

明日も頑張ります!!

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