# Inherit controller, how to do it?

**URL:** <https://www.ruby-forum.com/t/inherit-controller-how-to-do-it/103278>\
**Category:** Rails\
**Created:** [May 3, 2007, 6:53pm UTC](https://www.ruby-forum.com/t/inherit-controller-how-to-do-it/103278 "2007-05-03T18:53:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [May 3, 2007, 6:53pm UTC](https://www.ruby-forum.com/t/inherit-controller-how-to-do-it/103278/1 "2007-05-03T18:53:38Z")

</div>

Hello,

I’m doing small blog for myself, I faced a problem when I want some  
controller to inherit.

class BlogController \< ApplicationController

end

class PostController \< BlogController  
def list  
render :text =\> “asd”  
end  
end

class CommentController \< PostController  
def list  
render :text =\> “asd”  
end  
end

So when I visit

[http://domain.com/blog/post/list](http://domain.com/blog/post/list)

I get “asd”, shouldn’t it be like that?

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [May 3, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/inherit-controller-how-to-do-it/103278/2 "2007-05-03T19:09:10Z")

</div>

> end  
> [http://domain.com/blog/post/list](http://domain.com/blog/post/list)
> 
> I get “asd”, shouldn’t it be like that?

Nope. When you inherit a controller like you’ve done, all you are doing  
is giving your Comment/Post controllers access to the methods in  
BlogController.

So you could move your list() action up into BlogController, take it out  
of the other two and then call: [domain.com/post/list](http://domain.com/post/list) and  
domain/comment/list and you’d be _running_ the BlogController.list  
action.

To get that heiararchy you want something like this:

app/controllers/admin/admin\_controller.rb  
app/controllers/admin/photo\_controller.rb

that start off with these:

class Admin::AdminController \< ApplicationController  
class Admin::PhotoController \< Admin::AdminController

Then you can call [domain.com/admin/photo/](http://domain.com/admin/photo/)

-philip

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [May 3, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/inherit-controller-how-to-do-it/103278/3 "2007-05-03T19:57:20Z")

</div>

Philip H. wrote:

> To get that heiararchy you want something like this:
> 
> app/controllers/admin/admin\_controller.rb  
> app/controllers/admin/photo\_controller.rb
> 
> that start off with these:
> 
> class Admin::AdminController \< ApplicationController  
> class Admin::PhotoController \< Admin::AdminController
> 
> Then you can call [domain.com/admin/photo/](http://domain.com/admin/photo/)
> 
> -philip

Thanks, I was searching for that 😃

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [May 3, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/inherit-controller-how-to-do-it/103278/4 "2007-05-03T19:58:31Z")

</div>

But how does this reflect the views ?

I want also be able to put the different layouts like this

/views/blog/post/create.rhtml  
/views/blog/comment/create.rhtml

😃
