Is there any known problem in inheriting a Controller from another
controller (that is to say having an Admin::PostController <
PostController, with PostController < ApplicationController)?
It is something I haven’t seen done in tutorials or books, however in my
opinion (and as far as I can see from my tests) it should work.
Is there any known problem in inheriting a Controller from another
controller (that is to say having an Admin::PostController <
PostController, with PostController < ApplicationController)?
I am using inherited controllers, and I feel it made my code a lot
DRYier. Haven’t had any problem yet.
It is something I haven’t seen done in tutorials or books, however in
my opinion (and as far as I can see from my tests) it should work.
As far as I know there’s no known problems…but I haven’t searched
the bug list. I only say that because I’m using an inheritance scheme
close to what you’re doing. Except I’ve named my classes a little
differently.
I have mine set up (using your controller names) like this :
class AdminPostController < PostController
…class methods…
end
class PostController < ApplicationController
…class methods…
end
and it works fine. Is the scope operator (: absolutely necessary in
your case?
Is there any known problem in inheriting a Controller from another
controller (that is to say having an Admin::PostController <
PostController, with PostController < ApplicationController)?
It is something I haven’t seen done in tutorials or books, however in my
opinion (and as far as I can see from my tests) it should work.
Controller inheritance works fine, and is a good way to share code among
related controllers. For instance, putting your user authentication
filter in a superclass controller so all the subclasses are protected.
The only major caveat I’ve seen is that if you inherit an action from a
superclass controller that renders a view, you have to create a view
template for it in the subclass controller too, since ActionController
only looks for the view template in the class of the controller
instance, not by searching the views of the superclasses.
Controller inheritance works fine, and is a good way to share code
among
related controllers. For instance, putting your user authentication
filter in a superclass controller so all the subclasses are
protected.
I took some time to realize it. In fact if I were in a more traditional
framework (that is to say one where I had to write a lot more code), I’d
probably used inheritance from the beginning.
Rails is new to me, and I try to do things in “the right way”. So I
didn’t think to inheritance, since I hadn’t seen it. But actually it
seems a widespread tecnique.
The only major caveat I’ve seen is that if you inherit an action from
a
superclass controller that renders a view, you have to create a view
template for it in the subclass controller too, since
ActionController
only looks for the view template in the class of the controller
instance, not by searching the views of the superclasses.