简体   繁体   English

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

[英]before_filter in application controller for only get requests

I need to set up a before_filter in my ApplicationController that redirects a user if they have not yet agreed to a new terms of service. 我需要在我的ApplicationController中设置一个before_filter,如果用户尚未同意新的服务条款,则会重定向用户。 However, I want to restrict the filter to only run based on the type of request. 但是,我想将过滤器限制为仅根据请求类型运行。

I'd like to write something like this: 我想写这样的东西:

class ApplicationController

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

Is something like this possible? 这样的事情可能吗?

before_filter :do_if_get

private

  def do_if_get
    return unless request.get?

    # your GET only stuff here.
  end

Or more simply 或者更简单

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

private

  def get_filter
    # your GET only stuff here.
  end

Compliments to deefours answer, this also work with Sinatra::Request 赞美deefours回答,这也适用于Sinatra::Request

before do
  something if request.get?
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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