Peter Michaux
2006-May-31 16:20 UTC
[Rails] 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
Jon Gretar Borgthorsson
2006-May-31 16:28 UTC
[Rails] Sharing code between some controllers? Staying DRY
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:> 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- -------------- Jon Gretar Borgthorsson http://www.jongretar.net/
Stephen Bartholomew
2006-May-31 16:30 UTC
[Rails] Sharing code between some controllers? Staying DRY
Hi, Option 1 is the best i think - any reason why you''re overriding the redirect_to() method though? Steve Peter Michaux wrote:> 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Peter Michaux
2006-May-31 16:45 UTC
[Rails] Sharing code between some controllers? Staying DRY
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
Stephen Bartholomew
2006-May-31 16:53 UTC
[Rails] Sharing code between some controllers? Staying DRY
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 Peter Michaux wrote:> 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Peter Michaux
2006-May-31 17:33 UTC
[Rails] Sharing code between some controllers? Staying DRY
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