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.
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.
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.
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.