应用程序控制器中的before_filter仅用于获取请求

时间:2013-06-25 20:24:21

标签: ruby-on-rails ruby-on-rails-3 before-filter

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

我想写这样的东西:

class ApplicationController

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

这样的事情可能吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

before do
  something if request.get?
end