Inherit controller, how to do it?


#1

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

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


#2

end
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 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/

-philip


#3

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/

-philip

Thanks, I was searching for that :smiley:


#4

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

:smiley: