Model Relationships: Creating a Genre View
Master Rails MVC Architecture Through Genre Views
Core MVC Components in This Tutorial
Model Relationships
Working with three interconnected models: movies, genres, and their associations. Understanding how data flows between related entities.
Controller Logic
Creating resourceful routes and controller methods. Learning the show action pattern and parameter handling in Rails.
View Optimization
Building reusable partials and improving user interface. Creating clickable elements and attractive layouts.
Environment Setup Process
Navigate to Project Directory
Use Finder to locate your Rails class folder and open Terminal for command line operations.
Change Directory
Use the cd command with drag-and-drop functionality to navigate into your flix project folder.
Verify Project State
Ensure your Rails application is ready for the genre controller and view implementation.
Remember that controller names are always plural in Rails. This follows the RESTful convention where controllers manage collections of resources.
Generated Files and Routes
Controller Generation
Rails creates the genres_controller.rb file automatically along with associated view folders and basic structure.
Resourceful Routes
Using 'only: :show' restricts routes to just the show action, providing clean URL structure without unnecessary endpoints.
You now have two show.html.erb files in different folders. Rails' strict organizational system requires this naming, so be careful to edit the correct file in the genres views folder.
View Creation Process
Create View File
Save as show.html.erb in the app/views/genres directory that Rails automatically created during controller generation.
Add Genre Heading
Use ERB syntax to display the genre name as the main page heading with proper HTML structure.
Loop Through Movies
Iterate through all movies in the genre using the association, creating clickable links for each movie title.
Make Genres Clickable
Update the movie show view to turn genre names into clickable links using Rails link_to helper method.
Rather than duplicating code between index and genre pages, we're creating a reusable partial. This follows the Don't Repeat Yourself principle, making code more maintainable.
Partial Implementation Benefits
Partial Creation and Implementation
Extract Existing Code
Cut the movie list HTML from index.html.erb, specifically lines 14-30 that contain the ul tags and movie iteration.
Create Partial File
Save the extracted code as _movie_list.html.erb with the required underscore prefix to indicate it's a partial.
Update Variable References
Change @movies to movies in the partial to use local variables instead of instance variables for better reusability.
Render with Parameters
Update both index and genre views to render the partial while passing the appropriate movie collections as local variables.
When rendering partials from different controllers, use the full path like 'movies/movie_list'. Rails automatically handles the underscore prefix for partial files.
Key Takeaways
