# Module methods as controller actions

**URL:** <https://www.ruby-forum.com/t/module-methods-as-controller-actions/143655>\
**Category:** Rails\
**Created:** [July 21, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/module-methods-as-controller-actions/143655 "2008-07-21T16:16:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![blackflash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dfb087/32.png) [@blackflash](https://www.ruby-forum.com/u/blackflash)\
**Post date:** [July 21, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/module-methods-as-controller-actions/143655/1 "2008-07-21T16:16:46Z")

</div>

Is it possible to include module methods as controller actions and use  
them?

---

<div class="post-metadata">

**Author:** ![blackflash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dfb087/32.png) [@blackflash](https://www.ruby-forum.com/u/blackflash)\
**Post date:** [July 21, 2008, 4:29pm UTC](https://www.ruby-forum.com/t/module-methods-as-controller-actions/143655/2 "2008-07-21T16:29:03Z")

</div>

On 21 Jul 2008, at 15:16, blackflash wrote:

> Is it possible to include module methods as controller actions and use  
> them?

Yes.

Fred

---

<div class="post-metadata">

**Author:** ![blackflash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dfb087/32.png) [@blackflash](https://www.ruby-forum.com/u/blackflash)\
**Post date:** [July 21, 2008, 4:41pm UTC](https://www.ruby-forum.com/t/module-methods-as-controller-actions/143655/3 "2008-07-21T16:41:22Z")

</div>

On Jul 21, 4:27 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On 21 Jul 2008, at 15:16, blackflash wrote:
> 
> > Is it possible to include module methods as controller actions and use  
> > them?
> 
> Yes.
> 
> Fred

How? 🙂

I’ve tried something like this:

module ControllerPeople  
def index  
puts ‘index action’  
render :text =\> ‘OK’  
end

def self.included(base)  
base.send :helper\_method, :index  
end  
end

class PeopleController \< ActionController::Base  
include ControllerPeople  
end

but, I get no method error ‘index’ in PeopleController.

Thanks,  
Dalibor

---

<div class="post-metadata">

**Author:** ![blackflash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dfb087/32.png) [@blackflash](https://www.ruby-forum.com/u/blackflash)\
**Post date:** [July 22, 2008, 2:13pm UTC](https://www.ruby-forum.com/t/module-methods-as-controller-actions/143655/4 "2008-07-22T14:13:33Z")

</div>

> That’s odd. I’ve done exactly that (but without the self included  
> method - you don’t need that) and it worked fine.
> 
> Fred

Yup, it works that way. But the problem is when I use before\_filter in  
the controller to decide what module to be included.

Is there any solution for this or maybe any design pattern for  
polymorphic controllers (that are generated dynamically depending on  
some values in post data)?

---

<div class="post-metadata">

**Author:** ![blackflash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dfb087/32.png) [@blackflash](https://www.ruby-forum.com/u/blackflash)\
**Post date:** [July 21, 2008, 7:02pm UTC](https://www.ruby-forum.com/t/module-methods-as-controller-actions/143655/5 "2008-07-21T19:02:21Z")

</div>

On Jul 21, 3:41 pm, blackflash [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> end
> 
> class PeopleController \< ActionController::Base  
> include ControllerPeople  
> end
> 
> but, I get no method error ‘index’ in PeopleController.

That’s odd. I’ve done exactly that (but without the self included  
method - you don’t need that) and it worked fine.

Fred

---

<div class="post-metadata">

**Author:** ![blackflash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dfb087/32.png) [@blackflash](https://www.ruby-forum.com/u/blackflash)\
**Post date:** [July 22, 2008, 9:20pm UTC](https://www.ruby-forum.com/t/module-methods-as-controller-actions/143655/6 "2008-07-22T21:20:14Z")

</div>

On 22 Jul 2008, at 13:12, blackflash wrote:

> 

That’s doesn’t sound like a great idea - the modules that are included  
in the controller won’t magically go away at the end of the request  
(except in development mode)

> Is there any solution for this or maybe any design pattern for  
> polymorphic controllers (that are generated dynamically depending on  
> some values in post data)?

I’ve got the feeling you’re overthinking things. What’s wrong with

def some\_action  
if foo  
bar  
else  
baz  
end  
end

or just redirect the user to a different controller depending on the  
post data.

Fred

---

<div class="post-metadata">

**Author:** ![blackflash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dfb087/32.png) [@blackflash](https://www.ruby-forum.com/u/blackflash)\
**Post date:** [July 22, 2008, 10:56pm UTC](https://www.ruby-forum.com/t/module-methods-as-controller-actions/143655/7 "2008-07-22T22:56:29Z")

</div>

On Jul 22, 9:19 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> or just redirect the user to a differentcontrollerdepending on the  
> post data.
> 
> Fred

Redirect would not work since I’m actually writing an API. And the  
problem with the above code is that it’s kind of modularity  
programming. I should implement a versioning system for API (depending  
of the parameter in the request xml data, i.e. api\_versions=‘2’, that  
version of API responses)… I suppose only controller actions and  
their views would differ from version to version, and models are easy  
to handle with just extending the logic in them.

Yes, this could be done with interface, but I see it unnatural for  
rails MVC architecture.
