Rails File type html.erb, difference between <%%> and <% =%>

I just started digging into Ruby and Ruby on Rails, and I noticed two options for embedding Ruby syntax in .html.erb files:

<% #ruby code here %>

Or:

<%= #ruby code here %>

How to choose one of them?

+4
source share
2 answers

<%=outputs the result of Ruby. <%just evaluates Ruby.

<p>Hi! How are you? 1 + 1 = <%= 1 + 1 %></p>

Will be <p>Hi! How are you? 1 + 1 = 2</p>.

<p>Hi! How are you? 1 + 1 = <% 1 + 1 %></p>

It will become <p>Hi! How are you? 1 + 1 = </p>.

<%commonly used for flow control, for example. if/else. Example:

<% if model.nil? %>
  <%= render 'new_model_form' %>
<% else %>
  <%= render 'detail_view' %>
<% end %>

Read more at http://guides.rubyonrails.org/layouts_and_rendering.html

+6
source

<% = render, <% no.

0
source

All Articles