Master ruby with us

Creating Sub Controllers in RubyOnRails

Posted by firstruby on January 14, 2009

Previously I was adding most of the code in one controller…say if there is admin module, and there are different sections in it, then it’s better to add the code in separate controllers – sub-controllers.

Here is the way to create sub-controllers,

ruby script/generate controller admin/users

It will create users_controller.rb in /controllers/admin folder (sub-folder for controller)

Your user controller will look like,

class Admin::UsersController < ApplicationController

And to inherit the properties of the parent controller (AdminController), you can change parent controller as AdminController instead ApplicationController

class Admin::UsersController < AdminController

Hope this helps.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: