Chapter 5: Controllers and Views with ERB

Chapter 5: Controllers and Views with ERB

AI Reading

Quick summary of this article

This chapter explains how Rails controllers and views work together using ERB templates. Controllers are Ruby classes with actions that handle requests, while views are HTML files that can include embedded Ruby code to display dynamic content. Instance variables set in controller actions automatically become available in corresponding view files, allowing developers to pass data from the backend to the frontend.

  • Generate a controller and its matching view with the command rails generate controller pages home, which creates both a controller file and a view file.
  • Any instance variable (starting with @) set in a controller action is automatically accessible in the corresponding view.
  • ERB uses two main tags: <% ... %> for running Ruby logic without output, and <%= ... %> for displaying Ruby results in the HTML.
  • You can use Ruby conditionals and loops directly inside ERB templates to create dynamic content, such as checking values or iterating over arrays.
  • Practice involves generating a controller with an "about" action, setting an instance variable, and displaying it in the view using ERB tags.

Chapter 5: Controllers and Views with ERB

A controller is a Ruby class with methods called actions. When a request arrives, Rails runs the matching action and then renders a view with the same name. Views are HTML templates written in ERB (Embedded Ruby), which lets you mix HTML and Ruby together.

In this chapter you will learn how to generate controllers, pass data from the controller to the view, and use ERB tags to display dynamic content.

🔧 Generating a Controller

To create a controller called pages with an action called home, run:

rails generate controller pages home

This creates app/controllers/pages_controller.rb:

# app/controllers/pages_controller.rb
class PagesController < ApplicationController
  def home
  end
end

It also creates the view file app/views/pages/home.html.erb.

💾 Passing Data to the View

Any instance variable (starting with @) you set in an action is automatically available in the view:

# app/controllers/pages_controller.rb
class PagesController < ApplicationController
  def home
    @name = "Ruby Learner"
    @year = Time.now.year
  end
end

📝 ERB Tags

ERB has two main tags:

  • <% ... %> – runs Ruby code but prints nothing (for logic).
  • <%= ... %> – runs Ruby code and prints the result (for displaying values).
<h1>Welcome, <%= @name %>!</h1>
<p>The current year is <%= @year %>.</p>

<% if @year > 2020 %>
  <p>We are in the future!</p>
<% end %>

🔑 Looping in Views

You can loop over arrays directly in ERB using a <% ... %> block:

<ul>
  <% ["Ruby", "Rails", "HTML"].each do |topic| %>
    <li><%= topic %></li>
  <% end %>
</ul>

🏠 Practice Exercises

# 1. Generate a controller
rails generate controller pages about
# 2. Add an instance variable in the action
def about
  @title = "About This Course"
end
<!-- 3. Display it in app/views/pages/about.html.erb -->
<h1><%= @title %></h1>

In the next chapter you will learn how models and migrations connect Rails to a database.

Leave a Reply

Your email address will not be published. Required fields are marked *