开发者

Exact routing matching routes created by resources in Ruby on Rails

What are the equivalent matching routes created from the using a resources route?

Please be exact.

For example what would the equivalent matches for:

resources :users

There should be 7 different match routes matching the 7 actions - index, new, create, edit, update, show, and delete. Here is one of them:

Http Method: New (Get Action)

match "users/new", :to => "users#new", :via => :get, :as => :new_user

I have a few more but I would like to confirm what I have with 开发者_运维技巧the community. Thanks!


I think you wanted to know what the code should be in the routing file so...

match "users/:id", :to => "users#show", :via => :get, :as => :user
match "users", :to => "users#index", :via => :get, :as => :users
match "users", :to => "users#create", :via => :post
match "users/:id/edit", :to => "users#edit", :via => :get, :as => :edit_user
match "users/:id", :to => "users#update", :via => [:put, :patch]
match "users/new", :to => "users#new", :via => :get, :as => :new_user
match "users/:id", :to => "users#destroy", :via => :delete


You are correct, it does give 7 different routes. From Rails Guides

resources :users 

gives 7 different routes all of which would map to the Users controllers.

Those routes will be

GET     /photos             index       display a list of all photos
GET     /photos/new         new         return an HTML form for creating a new photo
POST    /photos             create      create a new photo
GET     /photos/:id         show        display a specific photo
GET     /photos/:id/edit    edit        return an HTML form for editing a photo
PUT     /photos/:id         update      update a specific photo
DELETE  /photos/:id         destroy     delete a specific photo
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜