Topic: How many controllers to use?

Designing the model structure of an application can be tricky, but at least one can usually rely on some fairly defined ideas on what objects/data the application should be dealing with.

However how does one decide how many controllers one needs?

The "Agile Web Development with Rails" book ends up using 3 controllers in its store example:
StoreController, AdminController and LoginController
This makes sense as in the example browsing and maintenance task/rights are clearly separated and one can see login as being a clearly separate topic...

However in the real world how does one decide what functionalities to put into a separate controller? Are there and guidelines or best practices?

For instance in an application that manages some kind of collection the browsing and admin parts can overlap as users will have the right to create new items etc. Would you still use separate controllers? Or rather would you rather try to have different controllers per each major functionality/topic/"view" that your application offers?
E.g. Using my favorite DVD collection example, would you have one controller for browsing collections, one for browsing/editing actors etc.? (But not one controller per model...)

Last edited by bjavor (2007-01-11 08:19:08)

Re: How many controllers to use?

Excellent question! How many controllers to have, and what controllers to have can be a very difficult decision early on. A few suggestions:

1. How the URLs look shouldn't be your top priority initially
2. Separate controllers based on their functionality, not how they are accessed.
3. Learn RESTful design.
4. Avoid multiple scopes in a controller

I would say the best practice (RESTful design) is to create a controller for each model. This controller will hold basic CRUD operations (create, read, update, destroy - like the scaffolding) for that model and not much more. Now, not every model needs one controller, and not every controller needs to control a model - this is just a general guideline.

bjavor wrote:

The "Agile Web Development with Rails" book ends up using 3 controllers in its store example:
StoreController, AdminController and LoginController
This makes sense as in the example browsing and maintenance task/rights are clearly separated and one can see login as being a clearly separate topic...

If I were to make this application, I would create a Products, Carts, LineItems, and Orders controllers. I would do quite a bit more changing on the models/structure of the app, but I won't get into that....

bjavor wrote:

For instance in an application that manages some kind of collection the browsing and admin parts can overlap as users will have the right to create new items etc. Would you still use separate controllers?

Nope, if you follow the above, you would group controllers based off of functionality, not how they are accessed. This makes it easier to avoid duplication between them (DRY).

bjavor wrote:

E.g. Using my favorite DVD collection example, would you have one controller for browsing collections, one for browsing/editing actors etc.? (But not one controller per model...)

If you are following a RESTful design, you would have one controller manage each model. One controller managing browsing/editing actors, one controller for browsing collections, etc.

There are quite a few resources on REST design, a google search and a search on these forums should turn up quite a bit. If you need specific references or have any more questions, feel free to ask!

Railscasts - Free Ruby on Rails Screencasts