Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Ruby on Rails - Understanding ERB

Introduction

Embedded Ruby (ERB) is a powerful and flexible templating system that allows you to embed Ruby code within HTML. In Rails, ERB is used to generate dynamic content in views, making it a core component for rendering HTML templates. This guide will introduce you to the basics of using ERB in Rails views.

Key Points:

  • ERB allows you to embed Ruby code within HTML templates.
  • ERB is commonly used in Rails views to render dynamic content.
  • This guide covers the basic syntax and usage of ERB in Rails.

ERB Syntax

ERB provides two types of tags for embedding Ruby code in your HTML templates:

  • <%= %>: Outputs the result of the Ruby code inside the tag.
  • <% %>: Executes the Ruby code inside the tag without producing any output.

Here is a basic example:

<!-- Example ERB code -->
<h1>Welcome to My Blog</h1>
<p>Today is <%= Date.today %>.</p>
                

Embedding Ruby Code

You can use ERB to embed Ruby code that generates dynamic content. Here is an example of embedding Ruby code to loop through an array and display each item:

<!-- Example ERB code -->
<h1>Articles</h1>
<ul>
  <% @articles.each do |article| %>
    <li><%= article.title %></li>
  <% end %>
</ul>
                

In this example, the @articles instance variable is iterated over, and the title of each article is displayed in an unordered list.

Conditional Statements

ERB allows you to use Ruby conditional statements to control the flow of your templates. Here is an example:

<!-- Example ERB code -->
<% if @user %>
  <h1>Hello, <%= @user.name %>!</h1>
<% else %>
  <h1>Hello, Guest!</h1>
<% end %>
                

In this example, the template displays a personalized greeting if the @user instance variable is present; otherwise, it greets the user as "Guest".

Helper Methods

Rails provides many helper methods that you can use in your views to simplify common tasks. Here is an example using the link_to helper:

<!-- Example ERB code -->
<%= link_to 'Home', root_path %>
                

In this example, the link_to helper generates an HTML link to the root path of the application.

Form Helpers

Rails provides form helpers to make it easy to create and manage forms. Here is an example using the form_with helper:

<!-- Example ERB code -->
<%= form_with model: @article do |form| %>
  <div>
    <%= form.label :title %>
    <%= form.text_field :title %>
  </div>
  <div>
    <%= form.label :body %>
    <%= form.text_area :body %>
  </div>
  <div>
    <%= form.submit 'Save' %>
  </div>
<% end %>
                

In this example, the form_with helper creates a form for the @article model with fields for the title and body, and a submit button.

Partial Templates

ERB allows you to break your templates into smaller, reusable partials. Here is an example:

<!-- Example ERB code -->
<!-- app/views/articles/_form.html.erb -->
<%= form_with model: @article do |form| %>
  <div>
    <%= form.label :title %>
    <%= form.text_field :title %>
  </div>
  <div>
    <%= form.label :body %>
    <%= form.text_area :body %>
  </div>
  <div>
    <%= form.submit 'Save' %>
  </div>
<% end %>

<!-- app/views/articles/new.html.erb -->
<h1>New Article</h1>
<%= render 'form' %>
                

In this example, the form for the @article model is placed in a partial template _form.html.erb and rendered in the new.html.erb template using the render method.

Conclusion

ERB is a powerful tool for creating dynamic content in your Rails views. By understanding the basic syntax and using ERB effectively, you can create flexible and maintainable templates for your Rails applications.