Controller and action design patterns

I’m very new to rails and don’t want to get started in the wrong
direction.

My problem is that I don’t know how I should structure my controllers
and actions. Is there a general rule for what is an action and what is a
controller? For example should I start off by creating a controller for
each page I have or do I create a controller for every table in the
database?

You should read up on Rails before proceding. It’s not something you
just
“jump into”. Get the Agile Web D. book and work through it.
There
is a ton of good information there.

Generally, a contorller is designed to handle all requests for a
specific
part of your application. You could have only one controller for a very
simple application, and you could have lots for a very complex
application.
It depends entirely on how you want to manage your code. I would not do
one
controller per page… I might consider doing one controller per table
(which is what you get if you do script/generate scaffold.

As you become more experienced, you’ll begin to understand where your
controller boundaries are. Good luck!

If you’re just starting out, consider a controller a collection of
related actions. Often, you’ll find that a controller is tied to a
model, with actions for listing, reading, creating, deleting and editing
entries. A controller might also handle related models (ie. a
blog_controller might also handle comments, categories and tags in
addition to blog posts). Furthermore, a controller might not be related
to a specific model at all. For instance: you might have a controller
named Dashboard that provides an overview of the app.

Here’s a couple of things to have in mind:

  • Don’t repeat yourself. Helpers and protected methods in a controller
    are shared across all it’s actions and views.
  • Think of the controller as a context for the action. Ie, you might
    want to see a blog post in a different way in the Admin controller
    compared to the Blog controller.

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