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?

More than 3 years have passed since last update.

【Rails】resourcesとresourceの違い

Posted at

表記ミスかと思ったらちゃんとresourceも存在した。

##resources

routes.rb
...
resources :posts
...
Prefix Verb              URI Pattern               Controller#Action
posts_path      GET  	/posts(.:format)	       posts#index
                POST	/posts(.:format)	       posts#create
new_post_path	GET  	/posts/new(.:format)	   posts#new
edit_post_path	GET	    /posts/:id/edit(.:format)  posts#edit
post_path	    GET	    /posts/:id(.:format)       posts#show
                PATCH	/posts/:id(.:format)	   posts#update
                PUT	    /posts/:id(.:format)	   posts#update
                DELETE	/posts/:id(.:format)	   posts#destroy

##resource

indexとid付きのパスが生成されない。

たとえば、/profileでは常に「現在ログインしているユーザー自身」のプロファイルを表示し、他のユーザーidを参照する必要がないとする。
引用:https://railsguides.jp/routing.html#%E5%8D%98%E6%95%B0%E5%BD%A2%E3%83%AA%E3%82%BD%E3%83%BC%E3%82%B9

単数形で定義するのに注意!

routes.rb
...
resource :post
...
Prefix Verb              URI Pattern               Controller#Action
new_post_path	GET  	/post/new(.:format)	      posts#new
edit_post_path	GET	    /post/edit(.:format)      posts#edit
post_path	    GET	    /post(.:format)           posts#show
                PATCH	/post(.:format)	          posts#update
                PUT	    /post(.:format)	          posts#update
                DELETE	/post(.:format)	          posts#destroy
                POST	/post(.:format)	          posts#create

どちらも同じコントローラーで同時に定義できる。

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?