# Common action for all the controllers

**URL:** <https://www.ruby-forum.com/t/common-action-for-all-the-controllers/160167>\
**Category:** Rails\
**Created:** [February 12, 2009, 1:42pm UTC](https://www.ruby-forum.com/t/common-action-for-all-the-controllers/160167 "2009-02-12T13:42:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![siddick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@siddick](https://www.ruby-forum.com/u/siddick)\
**Post date:** [February 12, 2009, 1:42pm UTC](https://www.ruby-forum.com/t/common-action-for-all-the-controllers/160167/1 "2009-02-12T13:42:00Z")

</div>

I am writing a small plugin for ruby on Rails. I faced a problem while  
create a common action for all the controllers. I found a solution for  
it, But i am not satisfied with the solution.

The solution that I got is :-

ActionController::Base.class\_eval do  
def common\_action  
render :text =\> “hai”  
end

def action\_init  
self.class.action\_methods.add “common\_action”  
end

before\_filter :action\_init

end

* * *

If you have any alternate solution, please provide.

---

<div class="post-metadata">

**Author:** ![siddick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@siddick](https://www.ruby-forum.com/u/siddick)\
**Post date:** [February 12, 2009, 2:07pm UTC](https://www.ruby-forum.com/t/common-action-for-all-the-controllers/160167/2 "2009-02-12T14:07:58Z")

</div>

maybe i’m completely confused about what you’re trying to do, but  
application-controller says the following:

# Filters added to this controller apply to all controllers in the

application.

# Likewise, all the methods added will be available for all

controllers.

---

<div class="post-metadata">

**Author:** ![siddick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@siddick](https://www.ruby-forum.com/u/siddick)\
**Post date:** [February 12, 2009, 2:19pm UTC](https://www.ruby-forum.com/t/common-action-for-all-the-controllers/160167/3 "2009-02-12T14:19:06Z")

</div>

MaD wrote:

> maybe i’m completely confused about what you’re trying to do, but  
> application-controller says the following:
> 
> # Filters added to this controller apply to all controllers in the
> 
> application.
> 
> # Likewise, all the methods added will be available for all
> 
> controllers.

From plugin librarys, we cann’t access the ApplicationController  
class.

---

<div class="post-metadata">

**Author:** ![siddick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@siddick](https://www.ruby-forum.com/u/siddick)\
**Post date:** [February 12, 2009, 3:21pm UTC](https://www.ruby-forum.com/t/common-action-for-all-the-controllers/160167/4 "2009-02-12T15:21:40Z")

</div>

sorry, just checked it. i gotta be like this:

# my\_plugin.rb:

module MyPlugin  
def self.included(base)  
base.extend(ClassMethods)  
end

```
module ClassMethods
  def my_action
    # ...
  end
end

```

end

irb(MyOtherController):001:0\> MyOtherController.methods.include?  
(‘my\_action’)  
=\> true

---

<div class="post-metadata">

**Author:** ![siddick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@siddick](https://www.ruby-forum.com/u/siddick)\
**Post date:** [February 12, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/common-action-for-all-the-controllers/160167/5 "2009-02-12T15:35:55Z")

</div>

> If you have any alternate solution, please provide.

I didn’t go the plugin route in our application, but opted for a form of  
“behavior by convention”

class GenericController \< ApplicationController

# here’s the standard index method

def index  
blah blah blah  
end

# here’s a new common method for all controllers

def common\_action  
moo moo moo  
end  
end

class Project \< GenericController

# inherits the “common\_action” action

# and if need be (if Project truly needs behavior different from

# the ‘standard’ we have for our app

def index  
# non-standard behavior for this class  
blah blah blah  
end  
end

---

<div class="post-metadata">

**Author:** ![siddick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@siddick](https://www.ruby-forum.com/u/siddick)\
**Post date:** [February 12, 2009, 3:06pm UTC](https://www.ruby-forum.com/t/common-action-for-all-the-controllers/160167/6 "2009-02-12T15:06:21Z")

</div>

that’s right, but you didn’t mention you were trying to write a  
plugin.  
in that case try to write it like this:

# my\_plugin.rb:

module MyPlugin  
def my\_action  
end  
end

# init.rb:

ActionController::Base.send :include, MyPlugin

hope i didn’t forget anything.
