0
0
Ruby on Railsframework~30 mins

Why views present data in Ruby on Rails - See It in Action

Choose your learning style9 modes available
Why Views Present Data in Rails
📖 Scenario: You are building a simple Rails web app that shows a list of books. The data about books is stored in the controller, but you want to display it nicely on a web page.
🎯 Goal: Learn how views in Rails present data passed from the controller to show it on a web page.
📋 What You'll Learn
Create a list of books data in the controller
Set a variable in the controller to hold the books
Use the view template to loop through the books and display their titles
Add a heading in the view to label the list
💡 Why This Matters
🌍 Real World
Web apps often separate data logic (controller) from display logic (view) to keep code clean and manageable.
💼 Career
Understanding how views present data is essential for building user interfaces in Rails web development jobs.
Progress0 / 4 steps
1
DATA SETUP: Create a list of books in the controller
In the BooksController, create a variable called @books and set it to an array with these exact strings: 'The Hobbit', '1984', 'Brave New World'.
Ruby on Rails
Need a hint?

Use @books = [ ... ] inside the index method.

2
CONFIGURATION: Prepare the view template
Create a file called index.html.erb in the app/views/books/ folder. Inside it, add a heading with <h1>Book List</h1>.
Ruby on Rails
Need a hint?

Use an <h1> tag with the text 'Book List'.

3
CORE LOGIC: Display the books in the view
In index.html.erb, below the heading, add a <ul> list. Use <% @books.each do |book| %> to loop through @books. Inside the loop, add <li><%= book %></li>. Close the loop with <% end %>.
Ruby on Rails
Need a hint?

Use ERB tags to loop and display each book inside <li> tags.

4
COMPLETION: Confirm the view renders the book list
Ensure the BooksController#index action renders the index.html.erb view that shows the heading and the list of books.
Ruby on Rails
Need a hint?

Make sure the controller and view code are present together to show the data.