5

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?

JohnDel
  • 2,092
  • 8
  • 35
  • 64
  • what's wrong with `put 'photos/:id(.:format)', :to => 'photos#update'` ? – jibiel Dec 09 '11 at 08:35
  • To be honest, I didn't try PUT verb but I didn't find something on the routes.rb so I thought that it might not works as the DELETE. – JohnDel Dec 09 '11 at 08:53

2 Answers2

15
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!

jibiel
  • 8,175
  • 7
  • 51
  • 74
  • Ok the problem is inside the devise scope then. I tried it inside: devise_scope :user do delete "/logout" => "devise/sessions#destroy" end And it didn't work. Any suggestions for that?? Thank you. – JohnDel Dec 09 '11 at 09:18
  • And what for do you need that? If you want custom devise routes you can try `devise_for :users, :path_names => { :sign_up => "signup", :sign_in => "login", :sign_out => "logout" }` – jibiel Dec 09 '11 at 09:25
  • It seems you want some deeper customization. You may want to update your question then to lure the right person here. – jibiel Dec 09 '11 at 09:31
  • I've tried that also. It adds "/users/logout" etc. I want it without the "users" prefix. I tried to ask something a little different because I asked => http://stackoverflow.com/questions/8418514/rails-devise-how-can-i-disable-some-default-routes and I didn't get any answers. – JohnDel Dec 09 '11 at 09:33
  • And how did you know this not working? `devise_scope :user do delete "logout", :to => "devise/sessions#destroy" end` and `<%= link_to "Sign out", '/logout', :method => :delete %>`. Works for me. – jibiel Dec 09 '11 at 09:40
  • It doesn't work, it says "Unknown action Could not find devise mapping for path "/logout". Maybe you forgot to wrap your route inside the scope block? For example: devise_scope :user do match "/some/route" => "some_devise_controller" end ". Restart your server and you propably see it too. I am using rails 3.1.3 and ruby 1.9.3 . I've tried many things, like changing the config.sign_out_via = :delete from the devise.rb but it didn't work either. – JohnDel Dec 09 '11 at 09:59
3

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]
Marek Příhoda
  • 11,108
  • 3
  • 39
  • 53