jeudi 19 février 2015

Share some before_filters between controllers

I have two parent controllers, one for an API, one for normal html pages.



class ApplicationController < ActionController::Base
...
end

class ApiController < ActionController::Metal
include AbstractController::Callbacks
...
end


I want to share some before_filter on both controllers. I tried something like this:



class ApplicationController < ActionController::Base
include MyFilters
end

class ApiController < ActionController::Metal
include MyFilters
end

module MyFilters
before_filter :filter1
before_filter :filter2

def filter1
end

def filter2
end
...
end


And also this:



module MyFilters
def self.included(klass)
klass.before_filter :filter1
klass.before_filter :filter2
end

def filter1
end

def filter2
end
...
end


But in both cases I receive:



undefined method `before_filter' for MyFilters:Module


What is the correct way to implement this?


Aucun commentaire:

Enregistrer un commentaire