What is the - at the <% end -%>? [duplicate] - ruby-on-rails

This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 4 years ago.
I am currently learning ruby-on-rails and I noticed in the tutorial I am following that closing the syntax <% end %>.
Out of curiosity, I wanted to know why mine is showing <% end -%> with the minus sign before %. The codes are working just fine with the minus sign?

<% end -%>
is used to avoid line break after expression

<% %> and <%- -%> are the same. however that <%= %> and <%= -%> are different: only the latter removes trailing whitespaces.
Example :
<div>
<%= "Hii" -%>
</div>
It will produce
<div>
Hii</div>
and without '-' :
<div>
Hii
</div>

Related

Use of <%= %> instead of <% %> ruby on rails [duplicate]

This question already has an answer here:
embedded ruby syntax "<%= .. %>" in rails [duplicate]
(1 answer)
Closed 8 years ago.
I've been following Agile Web Development with Rails, and their code suddenly switches from the use of <%= %> to <% %>.
Code Example 1:
<%= 1+2 %>
Code Example 2:
<% for file in #files %>
file name is: <%= file %>
<% end %>
My question is, for the second code sample why can I not use <%= %>?
Thanks!
In ERB (embedded ruby) syntax, <%= %> is shorthand for "perform the following ruby code AND THEN print the result". So in the first example, it will print the result of the operation 1 + 2, 3.
The second example shows a for loop which will iterate and print the contents between the for declaration and its corresponding <% end %>.
To answer your question, the expression for file in #files itself doesn't return anything worth printing so there's no need to use <%= %> and in fact doing so can cause a hard-to-track-down bug.

Rails html.erb filetype, difference between <% %> and <%= %> [duplicate]

This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 8 years ago.
I just began digging into Ruby and Ruby on Rails, and I noticed two options for embedding Ruby syntax within .html.erb files:
<% #ruby code here %>
Or:
<%= #ruby code here %>
How should I choose one over the other?
<%= outputs the result of the Ruby. <% just evaluates the Ruby.
<p>Hi! How are you? 1 + 1 = <%= 1 + 1 %></p>
Will become <p>Hi! How are you? 1 + 1 = 2</p>.
<p>Hi! How are you? 1 + 1 = <% 1 + 1 %></p>
Will become <p>Hi! How are you? 1 + 1 = </p>.
<% is generally used for flow control, ex. 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
<%= renders, <% does not.

Why many people use "-%>" instead of "%>" in Rails? [duplicate]

This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 8 years ago.
Sorry for this question, i think its more offtopic, but i couldn't find anything on google!
I saw now multiple times that a lot of people use -%> instead of just %>. Whats the sense?
Example:
<% #images.each_slice(6) do |slice| -%>
<div class="gallery">
<% slice.each do |image| -%>
<%= image_tag(image.url, :alt => image.alt) %>
<% end -%>
</div>
<% end -%>
Source: Rails each loop insert tag every 6 items?
Here he has also used -%> for all blocks.
I would like to add some resources that I know about ERB :
Rails extends ERB, so that you can suppress the newline simply by adding a trailing hyphen to tags in Rails templates:
<ul>
<% for #item in #items -%>
<li><%= #item %></li>
<% end -%>
</ul>
Comment markers use a hash sign:
<%# This is just a comment %>
A tag with an equals sign indicates that enclosed code is an expression, and that the renderer should substitute the code element with the result of the code (as a string) when it renders the template. Use an expression to embed a line of code into the template, or to display the contents of a variable:
Hello, <%= #name %>.
Today is <%= Time.now.strftime('%A') %>.
With one equal sign the string will be encoded. To avoid encoding, you can use two equals signs (or raw):
Hello, <%== #unencodedOutput %>
Tags without the equals sign denote that the enclosed code is a scriptlet. Each scriptlet is caught and executed, and the final result of the code is then injected in to the output at the point of the scriptlet.
<ul>
<% for #item in #shopping_list %>
<li><%= #item %></li>
<% end %>
</ul>
Scriptlets are most commonly used for embedding loops or conditional logic into templates:
Read An Introduction to ERB Templating to know more.

What is the difference between "-%>" and "%>" in Rails 3? [duplicate]

This question already has answers here:
'-%>' (minus sign) at the end of a ERb sequence
(2 answers)
Closed 8 years ago.
What is the difference between :
<% #posts.each do |p| -%>
<%= p.title %>
<% end -%>
and
<% #posts.each do |p| %>
<%= p.title %>
<% end %>
and is there any ?
-%> means that do not insert '\n' and whitespaces after the command.
There is no difference.
"-%>" is completely useless in Rails 3.

Ruby on Rails App Displaying Unwanted Data [duplicate]

This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 3 years ago.
Whenever the following code runs i get extra output on the page which looks like the garbage on the bottom. I have tried to see if it is in my migrations or function calls, but they all seem to be pretty clean (not much too f.name). If needed I can provide a copy of my migrations and schema.
<%= #subject.Name %>
<%= #subject.formulas.each do |f| %>
<div class="entry">
<h3><%= f.name %></h3>
</div>
<% end %>
The extra output is:
[#<Formula id: 2, name: "Pythagorean Theorem", description: "n any right triangle, the area of the square whose ...", formula: "a^2+b^2=c^2", created_at: "2011-05-17 03:18:44", updated_at: "2011-05-17 03:18:44">]
Change
<%= #subject.formulas.each do |f| %>
to
<% #subject.formulas.each do |f| %>

Resources