简体   繁体   中英

Can I limit the method in Ruby on Rails is POST method only?

For example:

class UsersController < ApplicationController 

 def doSomething

 end

 def doSomethingAgain

 end

end

Can I restrict the user pass a get method only to doSomething, but doSomethingAgain is only accept post method, can I do so?

class UsersController < ApplicationController 
  verify :method => :post, :only => :doSomethingAgain

  def doSomething
  end

  def doSomethingAgain
  end

end

You may specify in routes.rb

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

You may specify more then one method

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

Here example

resources :products do
  resource :category

  member do
    post :short
  end

  collection do
    get :long
  end
end

I think you'll be best off with using verify as Draco suggests. But you could also just hack it like this:

 def doSomethingAgain
   unless request.post?
     redirect_to :action => 'doSomething' and return
   end

   # ...more code
 end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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