我可以在Ruby on Rails中限制方法只是POST方法吗?

时间:2010-07-15 10:21:59

标签: ruby-on-rails ruby

例如:

class UsersController < ApplicationController 

 def doSomething

 end

 def doSomethingAgain

 end

end

我可以限制用户只将get方法传递给doSomething,但是doSomethingAgain只接受post方法,我可以这样做吗?

4 个答案:

答案 0 :(得分:5)

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

  def doSomething
  end

  def doSomethingAgain
  end

end

答案 1 :(得分:2)

您可以在routes.rb

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

您可以指定多个方法

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

答案 2 :(得分:0)

这里的例子

resources :products do
  resource :category

  member do
    post :short
  end

  collection do
    get :long
  end
end

答案 3 :(得分:0)

我认为你最好使用verify,正如Draco建议的那样。但你也可以像这样破解它:

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

   # ...more code
 end