# Controller Inheritance

**URL:** <https://www.ruby-forum.com/t/controller-inheritance/60483>\
**Category:** Rails\
**Created:** [May 14, 2006, 4:50pm UTC](https://www.ruby-forum.com/t/controller-inheritance/60483 "2006-05-14T16:50:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![riko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/919ad9/32.png) [@riko](https://www.ruby-forum.com/u/riko)\
**Post date:** [May 14, 2006, 4:50pm UTC](https://www.ruby-forum.com/t/controller-inheritance/60483/1 "2006-05-14T16:50:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![riko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/919ad9/32.png) [@riko](https://www.ruby-forum.com/u/riko)\
**Post date:** [May 14, 2006, 8:21pm UTC](https://www.ruby-forum.com/t/controller-inheritance/60483/2 "2006-05-14T20:21:22Z")

</div>

Riko [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read my blog at: [http://cuttingtheredtape.blogspot.com/](http://cuttingtheredtape.blogspot.com/)  
,----  
| “War is Peace! Freedom is Slavery! Ignorance is Strength!”  
| – Orwell, 1984, 1948  
`----

---

<div class="post-metadata">

**Author:** ![riko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/919ad9/32.png) [@riko](https://www.ruby-forum.com/u/riko)\
**Post date:** [May 14, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/controller-inheritance/60483/3 "2006-05-14T19:01:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![riko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/919ad9/32.png) [@riko](https://www.ruby-forum.com/u/riko)\
**Post date:** [May 15, 2006, 8:11am UTC](https://www.ruby-forum.com/t/controller-inheritance/60483/4 "2006-05-15T08:11:17Z")

</div>

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 S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![riko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/919ad9/32.png) [@riko](https://www.ruby-forum.com/u/riko)\
**Post date:** [May 15, 2006, 9:48am UTC](https://www.ruby-forum.com/t/controller-inheritance/60483/5 "2006-05-15T09:48:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![riko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/919ad9/32.png) [@riko](https://www.ruby-forum.com/u/riko)\
**Post date:** [May 15, 2006, 9:57am UTC](https://www.ruby-forum.com/t/controller-inheritance/60483/6 "2006-05-15T09:57:31Z")

</div>

Josh S. 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](http://www.akropolix.net/rik0/blogs) |  
site: [http://www.akropolix.net/rik0/](http://www.akropolix.net/rik0/) |  
forum: [http://www.akropolix.net/forum/](http://www.akropolix.net/forum/) |

---

<div class="post-metadata">

**Author:** ![riko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/919ad9/32.png) [@riko](https://www.ruby-forum.com/u/riko)\
**Post date:** [May 15, 2006, 12:04am UTC](https://www.ruby-forum.com/t/controller-inheritance/60483/7 "2006-05-15T00:04:20Z")

</div>

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 🙂
