# Common actions "split" amoung several controllers? how to?

**URL:** <https://www.ruby-forum.com/t/common-actions-split-amoung-several-controllers-how-to/77457>\
**Category:** Rails\
**Created:** [November 28, 2006, 2:33am UTC](https://www.ruby-forum.com/t/common-actions-split-amoung-several-controllers-how-to/77457 "2006-11-28T02:33:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [November 28, 2006, 2:33am UTC](https://www.ruby-forum.com/t/common-actions-split-amoung-several-controllers-how-to/77457/1 "2006-11-28T02:33:25Z")

</div>

Hi all -

I’ve got a set of common actions (for displaying photo thumbnails and  
their preview) that I’d like to share b/n a couple of controllers (ie.  
photos for people and photos for events).

I’m trying to figure out how best to extract this common functionality  
so  
it’s easy to update/manage (ie. dry).

The views aren’t a problem, I know how to do that, but I’m getting stuck  
with the controller/actions…

Right now I’ve got my actions in a _module_ that I’m _include_’ing in  
each  
controller. Works except I get the following when one of the protected  
methods (defined in the module) tries to use Paginator. It works if I  
move it into the controller.

My module is in lib/photos.rb and the error I’m getting is this:

uninitialized constant Paginator  
This error occured while loading the following files:  
photos/paginator.rb

Also, I’d like to setup a before\_filter for these common actions, but  
that  
doesn’t seem to work very well either.

Any ideas? Or other solutions?

Thanks!

-philip

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [November 28, 2006, 2:53am UTC](https://www.ruby-forum.com/t/common-actions-split-amoung-several-controllers-how-to/77457/2 "2006-11-28T02:53:31Z")

</div>

(skip down)

On Nov 27, 4:53 pm, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> with the controller/actions…  
> photos/paginator.rb  
> Well, this is fixed by explicitly calling  
> ActionController::Pagination::Paginator instead of just Paginator…

Still curious about best practices though…

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [November 28, 2006, 3:33am UTC](https://www.ruby-forum.com/t/common-actions-split-amoung-several-controllers-how-to/77457/3 "2006-11-28T03:33:34Z")

</div>

Hi –

On Mon, 27 Nov 2006, Philip H. wrote:

> The views aren’t a problem, I know how to do that, but I’m getting stuck  
> This error occured while loading the following files:  
> photos/paginator.rb
> 
> Also, I’d like to setup a before\_filter for these common actions, but that  
> doesn’t seem to work very well either.
> 
> Any ideas? Or other solutions?

What about putting them in application.rb? That way, they’ll be in  
the parent class of the controller classes.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [November 28, 2006, 4:21am UTC](https://www.ruby-forum.com/t/common-actions-split-amoung-several-controllers-how-to/77457/4 "2006-11-28T04:21:41Z")

</div>

Another option is to make your own parent class (like application.rb)  
and  
have your common controllers inherit from that.

You could also play around with mixing the actions in via a module. In  
theory that should work but I’ve not done it.

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [November 28, 2006, 4:19am UTC](https://www.ruby-forum.com/t/common-actions-split-amoung-several-controllers-how-to/77457/5 "2006-11-28T04:19:41Z")

</div>

> > Right now I’ve got my actions in a _module_ that I’m _include_’ing in each
> > 
> > Also, I’d like to setup a before\_filter for these common actions, but that  
> > doesn’t seem to work very well either.
> > 
> > Any ideas? Or other solutions?
> 
> What about putting them in application.rb? That way, they’ll be in  
> the parent class of the controller classes.

Hrm… good idea, but I think our app is too big to justify this…  
however, I could create another controller and have these controllers  
inherit from it… hrm… tempting 🙂

Thanks!

-philip
