MVC: Views: Free Ruby on Rails Tutorial
Master Ruby on Rails Views and Dynamic Data
Core MVC Components in Rails
Model
Handles data logic and database interactions. Defines the structure and behavior of your application's data entities.
View
Manages presentation layer and user interface. Renders HTML templates with embedded Ruby code for dynamic content.
Controller
Coordinates between models and views. Processes requests, handles routing, and prepares data for presentation.
Quick Setup Process
Navigate to Project Directory
Open Terminal and use cd command with drag-and-drop folder method for accurate path navigation
Clone Repository
Remove existing flix directory and clone fresh copy from Git repository using provided commands
Install Dependencies
Run bundle and yarn install commands to ensure all gems and JavaScript dependencies are properly configured
def show @movie = Movie.find(params[:id]) end
View files follow a strict naming pattern: action_name.html.erb. The show action corresponds to show.html.erb in the movies views directory.
Rails Helper Methods for Data Formatting
debug
Displays all available instance variables and their values. Essential for development and understanding data structure.
simple_format
Converts plain text to HTML with basic formatting. Transforms line breaks into paragraph tags automatically.
number_to_currency
Formats numerical values as currency with proper symbols and decimal places. Defaults to USD format.
image_tag
Generates HTML img tags with Rails asset pipeline integration. Supports CSS classes and accessibility attributes.
Static vs Dynamic Content Implementation
| Feature | Static HTML | Dynamic ERB |
|---|---|---|
| Title Display | <h2>Text M for Murder</h2> | <h2><%= @movie.title %></h2> |
| Image Source | src="/assets/posters/large/textmformurder-2x.jpg" | <%= image_tag "posters/large/#{@movie.poster_image}" %> |
| Price Format | 12.5 | <%= number_to_currency @movie.ticket_price %> |
Remember the difference: <%= %> prints output to screen, while <% %> executes code without output. This distinction is crucial for conditional statements and loops.
Dynamic Data Implementation Checklist
Ensures each page displays the correct movie name
Provides proper asset pipeline integration and accessibility
Creates professional price display with dollar signs and decimals
Converts line breaks to proper HTML paragraph structure
Controls exact date display format for user readability
Ruby date formatting can be complex. Bookmark apidock.com/ruby/DateTime/strftime for comprehensive formatting options and examples.
Key Takeaways
