Search code examples
ruby-on-railsruby-on-rails-4controllerbefore-filter

before_filter in ApplicationController not called (Rails 4)


I'd like to call a filter before every action of every controller of my rails application.

To achieve this, I've simply used before_filter inside my ApplicationController.

However, it doesn't work and my method is never called when I receive a valid request.

Here is the content of my 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

Even if before_filter is not supposed to be deprecated in rails 4, I've also tried to use before_action, but the result remains the same.

Any solution?


Solution

  • //edit: grml. i saw you already fixed your problem.

    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
    

    if i call the index action - its working perfectly. it is raising "i need to puke".

    please do me a favor: if you write methods called by a filter, they should be private.