5

如果用户尚未同意新的服务条款,我需要在我的 ApplicationController 中设置一个 before_filter 来重定向用户。但是,我想将过滤器限制为仅根据请求类型运行。

我想写这样的东西:

class ApplicationController

before_filter :filter, :only => {:method => :get}

这样的事情可能吗?

4

2 回答 2

8
before_filter :do_if_get

private

  def do_if_get
    return unless request.get?

    # your GET only stuff here.
  end

或者更简单地说

before_filter :get_filter, if: Proc.new {|c| request.get? }

private

  def get_filter
    # your GET only stuff here.
  end
于 2013-06-25T20:28:44.583 回答
0

赞美 deefours 的回答,这也适用于Sinatra::Request

before do
  something if request.get?
end
于 2014-08-19T22:26:04.260 回答