Forum: Ruby on Rails inherit controller, how to do it?

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.
Jamal S. (Guest)
on 2007-05-03 20:53
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?
Philip H. (Guest)
on 2007-05-03 21:09
(Received via mailing list)
>  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/<action goes here>

-philip
Jamal S. (Guest)
on 2007-05-03 21:57
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/<action goes here>
>
> -philip

Thanks, I was searching for that :D
Jamal S. (Guest)
on 2007-05-03 21:58
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

:D
This topic is locked and can not be replied to.