Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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. 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. Is it possible?

like image 556
JohnDel Avatar asked Dec 09 '11 08:12

JohnDel


2 Answers

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

or

resources :photos

and hit your app directory with

rake routes

C'mon, you're not even trying!

like image 69
jibiel Avatar answered Oct 04 '22 22:10

jibiel


Nothing prevents you from using put/post/delete 'directly', in place of match.

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):

match 'user/show' => 'users#show', :via => [:get, :post]
like image 38
Marek Příhoda Avatar answered Oct 04 '22 20:10

Marek Příhoda