Apply Rails controller filter to all controllers of a particular namespace?
-
05-07-2019 - |
Question
I have a bunch of controllers in a namespace Foo
.
I'd like to apply a before_filter 'require_user'
to all of them, but not to other controllers, that do not belong to Foo
namespace.
Is there any way to accomplish this besides explicitly invoking a before_filter method in each controller?
Solution
You could have a FooController
class which includes the before_filter
and then use that as the base class for all controllers in that namespace.
OTHER TIPS
No. Not other than doing some really nasty ObjectSpace
kind of hacks.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow