The Making of That Nutty Guy: Dynamic Content
Building Dynamic Ruby on Rails Applications
Core Concepts Covered
Dynamic Content
Transform static HTML into dynamic pages using Rails ERB templates and database-driven content rendering.
Partial Rendering
Create reusable code components that can be conditionally displayed across different pages and layouts.
Data Validations
Implement model-level validations to ensure data integrity and proper application behavior.
Moving the product listing code from the layout to the products index view follows Rails convention of keeping page-specific content in appropriate view files rather than shared layouts.
Every time we have a product, we're going to print one of these divs.
Dynamic Content Replacement
Use product.title to pull actual product names from the database
Replace hardcoded item numbers with product.sku for unique identification
Use number_to_currency helper for proper price display formatting
Use simple_format helper to preserve text formatting from database
Replacing static HTML file references with root path (/) ensures navigation works properly in the Rails routing system rather than looking for physical HTML files.
Conditional Partial Rendering
Extract Banner to Partial
Move the jumbotron banner code into a reusable partial file in the shared views directory.
Add Conditional Logic
Use instance variable @get_nutty to control when the banner appears in the layout.
Set Controller Variable
Define @get_nutty = true only in the products index action to show banner on homepage.
Partial filenames always begin with an underscore, but when rendering them in ERB templates, you reference them without the underscore prefix.
Dynamic Title Implementation
Controller Setup
Create @title instance variable in the show action to pass product-specific titles to the view layer.
Conditional Display
Use ERB conditional logic to show product name plus separator when @title exists, otherwise show default site name.
Key Takeaways