All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is the difference between <%, <%=, <%# and -%> in ERB templates (Ruby on Rails)?

This content has been automatically translated from Ukrainian.
In ERB (Embedded Ruby, *.erb files) templates used in Ruby on Rails, there are tags that define different types of embedded code: 
<%
<%=
<%#
-%>
They have the following meanings:
  • <% - this tag is used to insert Ruby code without outputting the result. It is used for logic and conditional expressions. For example, you can use it to create a loop or a conditional statement without directly outputting the result.
  • <%= - this tag is used to insert Ruby code with outputting the result. It is used to insert the value of a variable or the result of an expression directly into the output HTML code. For example, you can use it to display the value of a variable on the page.
  • <%# - this tag is used to ignore Ruby code (ERB comment) during the execution of the template. Everything between <%# and %> will be ignored and not outputted in the resulting HTML code. This is useful when you want to leave comments or temporarily disable a certain part of the code (during debugging).
For example:
<% if condition %>
  <%= variable %>
<% else %>
  <%# commented part %>
<% end -%>
  • -%> - This tag is used to control the whitespace after the closing tag. It is used to remove extra spaces or newline characters that are usually added after the tag %>. 
For example:
 <% 1 + 1 -%>
will output `2` without adding a space or newline character after it.
Tips:
<% %>  : Executes Ruby code
<%= %> : Outputs the given value in the ERB template
<% -%> : Removes spaces or newline characters after the expression
<%# %> : Commenting code
Like it?React
🧵

This post doesn't have any additions from the author yet.

Why does Ruby code return nil after executing puts?
May 29, '24 20:30

Why does Ruby code return nil after executing puts?

Enabling YJIT in Ruby 3.2.1 (Ruby on Rails)
May 8, '24 07:57

Enabling YJIT in Ruby 3.2.1 (Ruby on Rails)

What is the difference between nil and false in Ruby?
May 29, '24 20:59

What is the difference between nil and false in Ruby?

What is the difference between immediate value and reference in Ruby?
May 29, '24 12:00

What is the difference between immediate value and reference in Ruby?

How does the map method work in Ruby? An overview of the method's operation with examples.
Jul 30, '24 07:33

How does the map method work in Ruby? An overview of the method's operation with examples.

May 23, '23 06:57

What is debugging?

May 17, '23 18:52

What is a loop in Javascript? How do for and while loops work in Javascript?

May 23, '23 07:41

What are attr_accessor, attr_reader, and attr_writer in Ruby? What are they used for?

May 23, '23 11:16

How does the has_many through association (many to many) work in Ruby on Rails?

May 16, '23 22:17

How to remove the space between inline and inline-block elements?

May 24, '23 18:53

What are joins in Ruby on Rails and how does it work?

May 16, '23 20:02

What is Origin in Git?

Jun 2, '23 06:42

Error adding people to Google Family. Failed to load the page.

May 10, '23 17:40

How to use hex color codes in CSS with alpha values?

Jun 2, '23 12:53

What does super do in Ruby?

May 6, '23 09:19

How to disable File Upload in the Trix editor?

May 3, '23 18:48

How to get a random logical value true or false in Ruby?