# Calling a controller from another controller

**URL:** <https://www.ruby-forum.com/t/calling-a-controller-from-another-controller/113194>\
**Category:** Rails\
**Created:** [August 28, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/calling-a-controller-from-another-controller/113194 "2007-08-28T17:02:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [August 28, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/calling-a-controller-from-another-controller/113194/1 "2007-08-28T17:02:34Z")

</div>

How do I call the create method of b\_controller from a\_controller?

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [August 29, 2007, 1:03pm UTC](https://www.ruby-forum.com/t/calling-a-controller-from-another-controller/113194/2 "2007-08-29T13:03:39Z")

</div>

a couple things:

(A) if you want to specifically call the :create method for an object,  
it doesn’t matter which controller u call it from (i.e,  
ExampleClass.create… will work whatever controller u put it in) - so  
if this is what u want, u’r done

===============

(B) if you want to call any method from one controller to the other,  
consider the two possiblities:  
(B1) heirarchy -  
as all controllers inherit from ApplicationController, you can make a  
controller inherit from a different controller, which will pass the  
methods along the line of heirarchy, such as

Class ExampleController \< OtherController

def a  
b # will work  
“apples”  
end

end

Class OtherController \< ApplicationController

def b  
a # wont work  
“bannanas”  
end

end

===============

```
(B2) consider using a module; you can mix in methods from a module 

```

into both controllers, using require and inlcude respectively:

module MixMe # put this in the /lib folder

def a  
“carrots”  
end

def b  
“dung”  
end

end

require ‘mix\_me’  
Class ExampleController \< ApplicationController

include MixMe # mixes in the module

def some\_method  
a # works  
b # works  
render\_text “success…”  
end

end

require ‘mix\_me’  
Class OtherController \< ApplicationController

include MixMe # mixes in the same module

def some\_other\_method  
a # works  
b # works  
render\_text “success!”  
end

end

and, walla - should work perfectly.

===============

pick u’r choose.  
(D) good luck

🙂

shai

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [August 29, 2007, 1:49pm UTC](https://www.ruby-forum.com/t/calling-a-controller-from-another-controller/113194/3 "2007-08-29T13:49:13Z")

</div>

Hi, you can do the following:

class BController \< ActionController::Base

def create( … )  
…  
end

end

class AController \< ActionController::Base

def example\_method  
b\_controller = BController.new  
b\_controller.create( … )  
end

end

For additional examples of OOP in Ruby, please consult any of the  
available  
Ruby books.

Good luck,

-Conrad
