Forum: Ruby on Rails Controller Inheritance

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.
47d626d416d9da060a44ed9f684e32cf?d=identicon&s=25 Riko (Guest)
on 2006-05-14 16:50
(Received via mailing list)
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.
43358b1dc76c91f999cd7258873dbcb8?d=identicon&s=25 Brian Chamberlain (Guest)
on 2006-05-14 19:01
(Received via mailing list)
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?
D449d54c3b0f8c9930c11c7d7d3e6cdd?d=identicon&s=25 Surendra Singhi (Guest)
on 2006-05-14 20:21
(Received via mailing list)
Riko <rik0.py@gmail.com> writes:

> 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.

--
Surendra Singhi
http://ssinghi.kreeti.com, http://www.kreeti.com
Read my blog at: http://cuttingtheredtape.blogspot.com/
,----
| "War is Peace! Freedom is Slavery! Ignorance is Strength!"
|     -- Orwell, 1984, 1948
`----
47d626d416d9da060a44ed9f684e32cf?d=identicon&s=25 Riko (Guest)
on 2006-05-15 00:04
(Received via mailing list)
Brian Chamberlain wrote:

> As far as I know there's no known problems...

Good.

> Is the scope operator (::) absolutely necessary in
> your case?

Not necessary. I just wanted to. I grouped all the admin controllers.
This way I access them with

/admin/posts
/admin/users

Rails maps Admin::PostsController in /admin/posts and that's what I
wanted :)
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-05-15 08:11
Riko wrote:
> 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.

--
Josh Susser
http://blog.hasmanythrough.com
47d626d416d9da060a44ed9f684e32cf?d=identicon&s=25 Riko (Guest)
on 2006-05-15 09:48
(Received via mailing list)
Surendra Singhi wrote:

> I am using inherited controllers, and I feel it made my code a lot
> DRYier. Haven't had any problem yet.

Good. That's what I wanted to hear.
47d626d416d9da060a44ed9f684e32cf?d=identicon&s=25 Riko (Guest)
on 2006-05-15 09:57
(Received via mailing list)
Josh Susser wrote:

 > 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.

I noticed it. I think I'll have something like


            +------------+
            |   Post     |
            | Controller |
            +------------+
            /            \
    +------------+     +------------+
    | Admin::Post|     | Pub::Post  |
    | Controller |     | Controller |
    +------------+     +------------+

where the views exist only for the subclasses (along with the layouts)
and PostController has only some partials.


--
blog:  http://www.akropolix.net/rik0/blogs |
site:  http://www.akropolix.net/rik0/      |
forum: http://www.akropolix.net/forum/     |
This topic is locked and can not be replied to.