Sharing code between some controllers? Staying DRY

Hi,

I have four controllers: one for the store front and three for the
store admin. In each controller I have copied and pasted exactly the
same code. It is a method called redirect_to() to override Action
Controller’s redirect_to(). Copy and paste is bad. I can think of two
options but I don’t know how Rails will feel about them.

Option 1: Can I create an intermediate controller class like the
following? If so, are their pit falls to this method?

class ApplicationController

end

class AdminController < ApplicationController
private
def redirect_to()
end
end

class ProductController < AdminController

end

Option 2: I could create a flie in lib/ which contains the
redirect_to() method and then mix it into each controller. This option
doesn’t feel as good for some reason. Also I don’t know what code
would need to be in my controller file to do this.

Any ideas?

Thanks,
Peter

Well… I have used option 1 a lot. Feels fine.

class Admin::BaseController < ApplicationController
layout “admin”
before_filter :authorize, :except => :login
end

class Admin::MessagesController < Admin::BaseController

end

Allows me to do the before_filter for all the /admin/something
controllers in one place.

On 5/31/06, Peter M. [email protected] wrote:

class ProductController < AdminController
Thanks,
Peter


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Hi,

Option 1 is the best i think - any reason why you’re overriding the
redirect_to() method though?

Steve

Not strange really - i just wondered if it might be better to just
create your own method that calls redirect_to rather than overriding the
default. Further down the development of the application, you might
find that you need the original working of redirect_to which could cause
you problems if you’ve overridden it - you’d need to call the superclass
method explicitly.

Anyways - just a thought.

Steve

Hi Steve,

Thanks for the advice about controller inheritence.

On 5/31/06, Stephen B. [email protected] wrote:

any reason why you’re overriding the
redirect_to() method though?

Short story is I’m sending a redirect_to as a response to an ajax
request with status code 203. This will then prompt the browser to
make a new ajax request. Maybe sounds strange but it seems to be
working very well.

Peter

On 5/31/06, Stephen B. [email protected] wrote:

Not strange really - i just wondered if it might be better to just
create your own method that calls redirect_to rather than overriding the
default. Further down the development of the application, you might
find that you need the original working of redirect_to which could cause
you problems if you’ve overridden it - you’d need to call the superclass
method explicitly.

Anyways - just a thought.

And a very good thought. I haven’t decided exactly what I will do with
this redirect_to issue. This project is research into some new UI
ideas I haven’t seen before.

Either way I wanted to know about the controller inheritence.

Thanks again,
Peter