ruby on rails: weird array output [duplicate] - ruby-on-rails

This question already has answers here:
Why am I getting objects printed twice?
(4 answers)
Closed 6 years ago.
I have this code in on of my views
<%= #quotes.each do |f| %>
<%=f[:underwriter]%>: £<%=f[:premium]%>
<br>
<% end %>
And in my controller I pass it the argument
#quotes = [{underwriter:"dtc",premium:500},{underwriter:"abc",premium:800}]
I expect it to print out the underwriter and premium, which it does, however it tags the whole array on at the end for some reason. This is shown below
dtc: £500
abc: £800
[{:underwriter=>"dtc", :premium=>500}, {:underwriter=>"abc", :premium=>800}]
Anyone can enlighten my to what is causing this behaviour?

Just do
<% #quotes.each do |f| %>
<%=f[:underwriter]%>: £<%=f[:premium]%>
<br>
<% end %>
Array#each - Calls the given block once for each element in self, passing that element as a parameter. when iteartion completed, return the self. <%= %> prints what is in between inside the tag into erb file, whereas <% %> executes the ruby code within the brackets.
Check this one Rails, ERB syntax also.

Related

Iterating through array gives full array of items at the end [duplicate]

This question already has answers here:
Ruby / Rails - .each Iterator is printing entire array at the end of the loop [duplicate]
(2 answers)
Closed 5 years ago.
I am trying to iterate through a set of records. However, at the end of the iteration, rails displays the full array.
<%= #portfolio_item.technologies.each do |technology| %>
<p><%= technology.name %></p>
<% end %>
What shows up in the browser
Replace this line
<%= #portfolio_item.technologies.each do |technology| %>
with this
<% #portfolio_item.technologies.each do |technology| %>
<%= %> will evaluate the expression and returns the array.

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.

Why is .each repeating the array after completion in my rails view? [duplicate]

This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 3 years ago.
In my rails view page, I have the following loop that should loop through my tag_list array and print each tag:
<%= #user.profile.tag_list.each do |tag| %>
<%= tag %>
<% end %>
For some reason, it repeats the array after it prints each individual tag. For example, this array has two elements:
["ruby", "python"]
The output of the each method is "rubypython ruby,python". The output should just be "ruby python". How do I fix this?
By the way, I am using the acts-as-taggable-on gem to generate the tags, but that should not make a difference since it is just a simple array.
you should remove the equals sign
<%= #user.profile.tag_list.each do |tag| %>
to
<% #user.profile.tag_list.each do |tag| %>
the embedded ruby is printing your each block after it's run, so you're getting results of .each being run as well as the tag

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.

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