MVC: Controllers & Routing
Master Rails MVC Architecture and Routing Fundamentals
MVC Core Components
Model
Handles data storage and business logic. In Rails, models inherit from ActiveRecord and manage database interactions.
View
Presents data to users through templates. Rails views use ERB templates to dynamically render HTML content.
Controller
Coordinates between models and views. Controllers handle user requests and assign instance variables for views.
This exercise builds on previous Rails fundamentals. Complete exercises 3A-3B first to follow the natural progression from basic controllers to advanced routing concepts.
Environment Setup Process
Navigate to Project Directory
Open Finder and locate your Rails class files, then use Terminal to navigate to the project folder
Initialize Terminal Session
Type 'cd' followed by a space, then drag the project folder to Terminal and press Enter
Verify Project State
Ensure you're in the correct directory and have completed prerequisite exercises
Rails Field Types Overview
| Feature | Field Type | Use Case |
|---|---|---|
| string | Single-line text | Titles, names, short descriptions |
| text | Multi-line text | Long descriptions, content blocks |
| integer | Whole numbers | Counts, IDs, runtime minutes |
| decimal | Floating point numbers | Prices, ratings, percentages |
| boolean | True/false values | Feature flags, status indicators |
| date/time/datetime | Temporal data | Creation dates, scheduling |
Always set precision and scale for decimal fields. Use precision: 8, scale: 2 for monetary values to ensure consistent formatting across different database systems.
Rails migration filenames always start with a timestamp so Rails can run them in the proper order.
Non-Resourceful Routing Trade-offs
A single 'resources :movies' line creates multiple routes automatically, including index (/movies), show (/movies/:id), new, edit, create, update, and destroy actions.
Controllers must assign instance variables (with @ symbol) to share data with views. Without @movies = Movie.all, the view cannot access movie data.
Controller-View Data Flow Checklist
Create the method that will handle the specific route request
Use @ to make variables accessible outside the controller method
Build ERB file in correct views subdirectory matching controller name
Use the same @variable names in ERB templates to display data
Key Takeaways
