문제

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. 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

before do
  something if request.get?
end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top