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

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.

Related

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

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>

Ruby when to use <% or <%= for opening [duplicate]

This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 7 years ago.
Sorry I'm very new to Ruby and this is probably a simple question and I am just searching wrong for it.
For example:
<% flash.each do |key, value| %>
<%= content_tag :div, value, class: "alert alert-#{key}" %>
<% end %>
Why do we only use <% %> at the beginning and end of the loop, but <%= is used in the loop?
Another example, no loop involved:
<li><%= link_to "Contact Us", new_contact_path %></li>
Why am I using <%= instead of <% here?
Thanks for helping me with this newb question
You'll use <%= when you want the ERB to actually render code as a string in the browser, and <% when you simply want to execute Ruby code (e.g. performing an iteration, setting a variable, etc.)
In your examples:
<% flash.each do |key, value| %>
<%= content_tag :div, value, class: "alert alert-#{key}" %>
<% end %>
<% is used on flash.each to iterate over the flash variable, and then <%= is used to actually render a content_tag containing the values set when piping in key and value into the block.
Are you using erb?
<%> will execute the ruby code inside the brackets
<%=> will actually render some code in your view

How to concatenate items from loop [duplicate]

This question already has an answer here:
Rails separate array items with comma inside partial
(1 answer)
Closed 8 years ago.
I wonder if there's a quick way to concatenate the items from this view helper with a comma:
<% #projects.each do |project| %>
<%= link_to project.name, project_path(project) %>
<% end %>
so that I get:
Project name 1, Project name 2, Project name 3
Note that there's no comma at the end. Thanks for any ideas.
What about
<%= #projects.map { |p| link_to p.name, project_path(p) }.join(', ').html_safe %>
I added sanitize to p.name because otherwise if some special character is present in p.name it would break your HTML (and could lead to security issues, that is XSS attacks) I was wrong, I tried and it is escaped correctly.
<%= #projects.map {|project| link_to project.name, project_path(project)}.join(", ").html_safe %>

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.

Rails get index of "each" loop [duplicate]

This question already has answers here:
Finding out current index in EACH loop (Ruby) [duplicate]
(2 answers)
Automatic counter in Ruby for each?
(8 answers)
Closed 6 years ago.
So I have this loop:
<% #images.each do |page| %>
<% end %>
How would I get the index of "page" inside of the loop?
<% #images.each_with_index do |page, index| %>
<% end %>
The two answers are good.
And I also suggest you a similar method:
<% #images.each.with_index do |page, index| %>
<% end %>
You might not see the difference between this and the accepted answer. Let me direct your eyes to these method calls: .each.with_index see how it's .each and then .with_index.
Try each_with_index.

Resources