ApplicationController中的before_filter未调用(Rails 4)

时间:2015-08-30 09:44:23

标签: ruby-on-rails ruby-on-rails-4 controller before-filter

我想在我的rails应用程序的每个控制器的每个操作之前调用一个过滤器。

为实现这一目标,我在ApplicationController中使用了before_filter

然而,它没有工作,我收到有效请求时从未调用过我的方法。

以下是我的ApplicationController的内容:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_filter :authenticate_with_token

  def authenticate_with_token
      begin
          auth = AuthenticatorService.new
          auth.authenticate params[:email], params[:token], request.method, request.fullpath
          @current_user = auth.user
          return true
      rescue Exception => e
          return false
      end
  end

end

即使在rails 4中不应该弃用before_filter,我也尝试使用before_action,但结果仍然相同。

任何解决方案?

1 个答案:

答案 0 :(得分:2)

//编辑: GRML。我看到你已经解决了你的问题。

class ApplicationController < ActionController::Base
  before_filter :throw_up
  before_filter {
    raise "hi friend"
  end
  private
    def throw_up
      raise "i need to puke"
   end
end

class PagesController < ApplicationController
    def index
    end
end

如果我调用索引操作 - 它的工作完美。 它正在提高&#34;我需要呕吐&#34;。

请帮我一个忙:如果你编写过滤器调用的方法,它们应该是私有的。

相关问题