简体   繁体   English

Rails - 从路由指定http动词PUT或DELETE

[英]Rails - Specify http verb PUT or DELETE from the route

Is it possible to specify a put or delete http verb from the route.rb, for a specific path? 对于特定路径,是否可以从route.rb指定put或delete http动词?

Like: 喜欢:

get 'photos/show'

I tried to do it with match like the below: 我尝试用匹配如下:

match 'photos/show' => 'photos#show', :via => :delete

In the rake routes it seems right but it doesn't do a delete http request. 在rake路线中,它似乎是正确的,但它没有删除http请求。 Also I tried: 我也尝试过:

match 'photos/show' => 'photos#show', :via => :random

And in the rake routes its shows "random" 在耙子路线上它的节目“随机”

It seems that you can specify get or post as shown in the guides , but I don't know if I can specify put or delete. 您似乎可以指定get或post,如指南中所示,但我不知道是否可以指定put或delete。 Is it possible? 可能吗?

put 'photos/:id(.:format)', :to => 'photos#update'
delete 'photos/:id(.:format)', :to => 'photos#destroy'

or 要么

resources :photos

and hit your app directory with 然后点击你的app目录

rake routes

C'mon, you're not even trying! 来吧,你甚至没有尝试过!

Nothing prevents you from using put/post/delete 'directly', in place of match. 没有什么能阻止你直接使用put / post / delete'代替匹配。

As an example, this works for me: 举个例子,这对我有用:

get 'user/edit' => 'users#edit', :as => :edit_current_user
put 'signup' => 'users#update'

The via option is useful when you want to route like eg this (not a frequent case, though): 当您想要像这样(但不是常见的情况)路由时,via选项很有用:

match 'user/show' => 'users#show', :via => [:get, :post]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM