# Plugin : adding filters in ApplicationController

**URL:** <https://www.ruby-forum.com/t/plugin-adding-filters-in-applicationcontroller/184551>\
**Category:** Rails\
**Created:** [March 4, 2010, 11:30am UTC](https://www.ruby-forum.com/t/plugin-adding-filters-in-applicationcontroller/184551 "2010-03-04T11:30:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![onionwushu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5daacb/32.png) [@onionwushu](https://www.ruby-forum.com/u/onionwushu)\
**Post date:** [March 4, 2010, 11:30am UTC](https://www.ruby-forum.com/t/plugin-adding-filters-in-applicationcontroller/184551/1 "2010-03-04T11:30:58Z")

</div>

Hi everybody !

I’m trying to extend the class ApplicationController with a plugin to  
add filters to follow some executions. I don’t know why, but I observed  
a weird behaviour and I don’t know why. Here is my code.

# init.rb

require ‘myplugin’

# myplugin.rb

ApplicationController.class\_eval do  
before\_filter do  
puts “I’m in the before\_filter”  
end  
end

The things is this filter happens just the first time (the first action  
after Webrick is loaded). Well, I tried to do the same thing, extending  
ActionController::Base, and it works without problems. But I’d like to  
extend ApplicationController and not the Base.

Any ideas ?

---

<div class="post-metadata">

**Author:** ![onionwushu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5daacb/32.png) [@onionwushu](https://www.ruby-forum.com/u/onionwushu)\
**Post date:** [March 4, 2010, 6:15pm UTC](https://www.ruby-forum.com/t/plugin-adding-filters-in-applicationcontroller/184551/2 "2010-03-04T18:15:02Z")

</div>

On Mar 4, 10:30 am, onion wushu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> myplugin.rb  
> extend ApplicationController and not the Base.

In development mode your code gets reloaded between requests - the  
reloaded application controller class doesn’t have this filter added  
to it.

One way of working around this is for your plugin to add a method to  
base (eg add\_my\_filter) and call that from whichever controllers use  
it.  
There is also a hook ( config.to\_prepare ) that it called before each  
request in development mode

Fred
