Forum: Ruby on Rails Sharing code between some controllers? Staying DRY

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eea3feaacbe44706164289d068d94828?d=identicon&s=25 Peter Michaux (Guest)
on 2006-05-31 18:24
(Received via mailing list)
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
30ee518e6fdc5b07e060775b5a542bdb?d=identicon&s=25 Jón Borgþórsson (jongretar)
on 2006-05-31 18:31
(Received via mailing list)
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 Michaux <petermichaux@gmail.com> wrote:
>
> class ProductController < AdminController
> Thanks,
> Peter
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
882cc23c77c5c6d27613c51396a02a0d?d=identicon&s=25 Stephen Bartholomew (Guest)
on 2006-05-31 18:34
(Received via mailing list)
Hi,

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

Steve
Eea3feaacbe44706164289d068d94828?d=identicon&s=25 Peter Michaux (Guest)
on 2006-05-31 18:47
(Received via mailing list)
Hi Steve,

Thanks for the advice about controller inheritence.

On 5/31/06, Stephen Bartholomew <sb@2404.co.uk> 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
882cc23c77c5c6d27613c51396a02a0d?d=identicon&s=25 Stephen Bartholomew (Guest)
on 2006-05-31 18:59
(Received via mailing list)
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
Eea3feaacbe44706164289d068d94828?d=identicon&s=25 Peter Michaux (Guest)
on 2006-05-31 19:37
(Received via mailing list)
On 5/31/06, Stephen Bartholomew <sb@2404.co.uk> 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
This topic is locked and can not be replied to.