开发者

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

开发者 https://www.devze.com 2023-01-07 02:02 出处:网络
For example: class UsersController < ApplicationController def doSomething end def doSomethingAgain

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 o开发者_开发问答nly 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
0

精彩评论

暂无评论...
验证码 换一张
取 消