Chapter 4. Managing Data Flow: Controllers and Models
Remove all the traffic lights, yellow lines, one-way systems, and road markings, and let blissful anarchy prevail. I imagine it would produce a kind of harmony.
Sadie Jones
Sadie Jones’s advice notwithstanding, Rails does not share her affection for blissful anarchy. It’s time to meet the key player in Rails applications. Controllers are the components that determine how to respond to user requests and coordinate responses. They’re at the heart of what many people think of as “the program” in your Rails applications, though in many ways they’re more of a switchboard. They connect the different pieces that do the heavy lifting, providing a focal point for application development. The model is the foundation of your application’s data structures, which will let you get information into and out of your databases.
Warning
Controllers are important, certainly a “key player,” but don’t get too caught up in them. When you’re coming from other development environments, it’s easy to think that controllers are the main place you should put application logic. As you get deeper into Rails, you’ll likely learn the hard way that a lot of code you thought belonged in the controller really belonged in the model, or sometimes in the view.
Getting Started, Greeting Guests
Controllers are Ruby objects. They’re stored in the app/controllers directory of your application. Each controller has a name, and the object inside of the controller ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access