1

I have a set of controllers in the folder /controllers/admin that all look like this and having the same filter:

module Admin
  class UsersController < ApplicationController
    before_action :some_method

    #actions
  end
end

How could each namespaced controller inherit the before_action :some_method from a central place?

Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
  • The class is essentially the same as **Admin::UsersController**, so you can have other controllers nested from that like *class Admin::RestrictedUsersController < Admin::UsersController*. – Anatoly Nov 06 '15 at 21:32
  • Ah sorry. What I meant was that I have 8 "admin-controllers" already with that exact `filter` - is there any way to extract it and let all 8 controllers inherit it from one single place? – Fellow Stranger Nov 06 '15 at 21:37

1 Answers1

1

It seems you need an individual Base controller within Admin module namespace:

class Admin::BaseController < ApplicationController
  before_action :some_method

  #actions
end

class Admin::UsersController < Admin::BaseController
  #some_method filter is invoked here
end

class Admin::PostsController < Admin::BaseController
  #some_method filter is invoke here
end
Anatoly
  • 15,298
  • 5
  • 53
  • 77