资源的匹配和动词等价物是什么?

时间:2012-07-22 19:40:27

标签: ruby-on-rails routes

routing guide中它表示“路由文件中的单个条目,例如resources :photos在您的应用中创建八个不同的路由,所有映射到照片控制器:“。

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

如何使用match和动词方法(getpostpatchputdelete创建等效路线?

1 个答案:

答案 0 :(得分:4)

match '/photos'          => 'photos#index',   via: :get
match '/photos'          => 'photos#create',  via: :post
match '/photos/new'      => 'photos#new',     via: :get, as: 'new_photo'
match '/photos/:id/edit' => 'photos#edit',    via: :get, as: 'edit_photo'
match '/photos/:id'      => 'photos#show',    via: :get, as: 'photo'
match '/photos/:id'      => 'photos#update',  via: :patch
match '/photos/:id'      => 'photos#update',  via: :put
match '/photos/:id'      => 'photos#destroy', via: :delete

get    '/photos',          to: 'photos#index'
post   '/photos',          to: 'photos#create'
get    '/photos/new',      to: 'photos#new',  as: 'new_photo'
get    '/photos/:id/edit', to: 'photos#edit', as: 'edit_photo'
get    '/photos/:id',      to: 'photos#show', as: 'photo'
patch  '/photos/:id',      to: 'photos#update'
put    '/photos/:id',      to: 'photos#update'
delete '/photos/:id',      to: 'photos#destroy'