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

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

(skip down)

On Nov 27, 4:53 pm, Philip H. [email protected] 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…

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 | [email protected]
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 | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

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.

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 :slight_smile:

Thanks!

-philip