MVC: Creating a Model, View, & Controller
Master Ruby on Rails MVC Architecture Fundamentals
This tutorial moves beyond Ruby theory into practical Rails application development, building a real movie management system similar to Fandango or Rotten Tomatoes.
MVC Architecture Components
Model (ActiveRecord)
Stores and manages data. Provides methods to access and edit information like recipe.title and recipe.prep_time. Handles movies, cast members, and comments.
View (ActionView)
Displays information to users through templates. Formats data for presentation and receives user input. Each major component has its own view series.
Controller (ActionController)
Bridges models and views. Routes requests, contacts models for data, and passes information to views. Manages user interfaces and form submissions.
Components should be distinct with clear responsibilities. Models never render HTML, views never contain business logic, and controllers only coordinate between them.
Rails Project Setup Process
Navigate to Project Directory
Open Terminal, type cd with space, drag your Rails Class folder from Finder to Terminal, then press Return
Generate New Rails Application
Run 'rails new flix' command to create all basic Rails app components automatically
Enter Project Directory
Change into the new directory with 'cd flix' and explore created files using 'ls' command
These generated files provide structure without content—it's your responsibility to implement the specific functionality. While you could manually create these files and directories, the Rails generator ensures perfect naming conventions and file placement. Rails follows "Convention over Configuration" principles, meaning file names and locations must be precisely correct for automatic loading and routing to work. Even experienced developers use generators to avoid typos and maintain consistency across large applications.
Let's start the Rails development server to examine our current application state:
rails server
The Rails server provides live reloading and detailed error reporting, essential tools for efficient development workflows.
In a browser window, navigate to localhost:3000 or reload if you're already there. The Rails welcome page appears, confirming your application is running correctly.
Now navigate to localhost:3000/movies and observe the Routing Error page. This error is expected—generating a controller creates the class file but doesn't automatically configure URL routing. Rails routing is explicit and intentional, giving you complete control over your application's URL structure and preventing accidentally exposed functionality.
The routing system bridges the gap between URLs and controller actions. When users request localhost:3000/movies, Rails consults the routing configuration to determine which controller and action should handle the request. Let's configure this routing now.
Open the flix folder in your preferred code editor. Modern editors like Visual Studio Code, RubyMine, or Sublime Text provide syntax highlighting, auto-completion, and Rails-specific features that significantly improve development efficiency.
In your code editor, open flix > config > routes.rb. This file is the central routing configuration for your entire application.
Replace all the commented-out code with the following route definition:
Rails.application.routes.draw do
get 'movies' => 'movies#about'
end
This route declaration tells Rails to respond to GET requests for the /movies path by calling the about action (method) in the MoviesController class. The => syntax creates this mapping relationship, effectively saying "route GET /movies to MoviesController#about".
Save the file and switch back to your browser.
Navigate to localhost:3000/movies or reload the page. You'll see a new error message—progress! Rails now recognizes the movies route but reports that the about method doesn't exist in MoviesController. This step-by-step error resolution is typical in Rails development, with each error message guiding you toward the solution.
Open flix > app > controllers > movies_controller.rb in your code editor.
Examine the file structure: MoviesController is a Ruby class that inherits from ApplicationController. This inheritance chain provides extensive functionality—ApplicationController (located in application_controller.rb) inherits from ActionController::Base, which implements request handling, parameter processing, rendering capabilities, security features, and much more. This inheritance eliminates thousands of lines of boilerplate code from your application.
Add the about method to your controller:
class MoviesController < ApplicationController
def about
end
end
This empty method demonstrates Rails' "Convention over Configuration" philosophy. When a controller action completes without explicitly rendering a response, Rails automatically looks for a view template matching the action name. This implicit behavior reduces code complexity while maintaining predictable functionality.
Save the file and reload localhost:3000/movies in your browser.
The new error message indicates that Rails can't locate a template for the about action. Rails is searching for view templates in conventional locations based on the controller and action names. This automatic template resolution is part of Rails' magic—it reduces configuration while maintaining flexibility.
Every public method in a Rails controller expects to render some form of response, whether HTML, JSON, XML, or redirects to other actions. The framework's built-in intelligence, inherited from ActionController::Base, handles this automatically when you follow naming conventions.
Let's create the missing view template. In your code editor, create a new file.
Add the following HTML content:
<h1>About Flix</h1>
<p>Flix is all about movies!</p>
This simple content will verify that our complete MVC chain is functioning correctly. In production applications, view templates combine static HTML with dynamic Ruby code to create rich, data-driven interfaces.
Choose File > Save As from your editor menu.
Navigate to flix > app > views > movies directory.
Save the file as about.html.erb. The .html.erb extension indicates this is an ERB (Embedded Ruby) template that can contain both HTML and Ruby code.
Return to your browser and reload localhost:3000/movies.
Success! Your Flix application now displays the about page, demonstrating a complete request-response cycle through the MVC architecture. You've created what Rails calls a non-resourceful route—a custom route for specific functionality. As we progress, you'll learn about resourceful routes that automatically generate standard CRUD (Create, Read, Update, Delete) operations with minimal code.
The current appearance is intentionally minimal—Rails focuses on functionality over aesthetics in its default state. Professional Rails applications implement comprehensive design systems, responsive layouts, and sophisticated user interfaces. Styling and design implementation will be our focus in the next exercise, where we'll transform this basic structure into a polished web application.
In Terminal, press CTRL+C to stop the development server when you're ready to continue.
Generated Controller Files and Their Purpose
The main controller file that handles request processing and routing logic
Folder structure for views, though no actual view files are generated yet
Automated testing file for controller functionality validation
Helper functions for view formatting like date formatting and array transformations
Empty stylesheet files ready for custom styling and design implementation
Rails generate ensures correct file naming and placement, which is critical for Rails applications to function properly. It saves significant time compared to manual file creation.
Key Takeaways
