When to make a new controller

Hey all,

Ok this is probably a newb question, but I haven’t been programming with
MVC for very long, and I’ve been wondering what the general opinion is,
or best practices, for making a new controller. Like what cases would
you need a new controller for and what cases can you just put everything
into one controller?

Thanks in advance,
Randal

Randal,

It really depends on your preferences… I tend to design controllers
around “units of functionality”. I find that if I have one controller
juggling too many models, the code becomes cluttered…

For example, I could end up with a blogging controller that has list(),
list_articles() when it could be simply split up amongst 2 controllers:
controller1.list(), controller2.list(). The added benefit is that it’s
much easier to work with the scaffolding than to fight it… :slight_smile:

Please be advised that I am not an expert in this area

ilan

Randal S. wrote:

Hey all,

Ok this is probably a newb question, but I haven’t been programming with
MVC for very long, and I’ve been wondering what the general opinion is,
or best practices, for making a new controller. Like what cases would
you need a new controller for and what cases can you just put everything
into one controller?

Thanks in advance,
Randal

If you’re just starting out, keep it simple and create a separate
controller for each use case in your application.

Randal S. wrote:

Hey all,

Ok this is probably a newb question, but I haven’t been programming with
MVC for very long, and I’ve been wondering what the general opinion is,
or best practices, for making a new controller. Like what cases would
you need a new controller for and what cases can you just put everything
into one controller?

Thanks in advance,
Randal

Thanks guys. I’ll keep that in mind!

Mick S. wrote:

If you’re just starting out, keep it simple and create a separate
controller for each use case in your application.

Randal S. wrote:

Hey all,

Ok this is probably a newb question, but I haven’t been programming with
MVC for very long, and I’ve been wondering what the general opinion is,
or best practices, for making a new controller. Like what cases would
you need a new controller for and what cases can you just put everything
into one controller?

Thanks in advance,
Randal

Be careful here… you don’t want to confuse controller’s with their
actions. Controllers are a way to group user-focused functionality,
but unless you are designing extremely broad use cases, each of your
apps controllers will likely impact far more than a single use case.

Since people like to use the “blog app” example, I would recommend
downloading the Typo source code and looking at how the Controllers
and Models are laid out in there. It’s a pretty good balance, with
probably a few more controllers than is actually needed (not counting
the admin stuff, since admin interfaces are very different from user
interfaces), but still a very good organization example.

-Brian

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs