Can I restrict a method in Ruby on Rails to just the POST method?

For instance:

class UsersController < ApplicationController def doSomething end def doSomethingAgain end end 

Can I restrict the user to use the get method only for doSomething, but doSomethingAgain only accepts the post method, can I do this?

+4
source share
4 answers
 class UsersController < ApplicationController verify :method => :post, :only => :doSomethingAgain def doSomething end def doSomethingAgain end end 
+5
source

You can specify in routes.rb

 map.resources :users, :collection=>{ :doSomething= > :get, :doSomethingAgain => :post } 

You can specify more than one method

 map.resources :users, :collection=>{ :doSomething= > [:get, :post], :doSomethingAgain => [:post, :put] } 
+2
source

Here is an example

 resources :products do resource :category member do post :short end collection do get :long end end 
0
source

I think you will be better off using verify , as Draco suggests. But you can also just hack it like this:

  def doSomethingAgain unless request.post? redirect_to :action => 'doSomething' and return end # ...more code end 
0
source

Source: https://habr.com/ru/post/1315783/


All Articles