This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 3 years ago.
I'm listing a set of data (for simplicity sake, just the identity column) of a particular database table table as follows:
<%= #fields.select{|field| field.model=="PreferredOffering"}.each do |field| %>
<%= field.id %>
<br/>
<% end %>
As you may have gathered from above, I'm using the combination of select and each to iterate ONLY through rows whose column model contains the string PreferredOffering.
My expectation was that I would see a nice ordered list of numbers and indeed I do. My confusion is that I ALSO see the entire #fields array coughed all over the page, below the list of numbers. (See below html excerpt)
106
<br/>
107
<br/>
108
<br/>
109
<br/>
110
<br/>
111
<br/>
112
<br/>
[#<PreferredOfferingField id: 5, field_heading: "Anti-dilution provisions- Typical Weighted Average", category: "Anti-Dilution", intra_cat_order: 1, model: "P
My guess is that I'm doing something funny with select as I'm not really familiar with its usage.
Any ideas on how to remedy this would be received gratefully; thanks in advance.
<% %> Executes the Ruby code inside
<%= %> Prints the results
You are displaying array and then it's values, so you would want to change <%= %> to <% %> .
<%= #fields.select{|field| field.model=="PreferredOffering"}.each do |field| %>
to
<% #fields.select{|field| field.model=="PreferredOffering"}.each do |field| %>
Related
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>
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.
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.
This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 3 years ago.
I'm trying to print a simple array defined in my controller into my view with a new line for each element. But what it's doing is printing the whole array on one line.
Here's my controller:
class TodosController < ApplicationController
def index
#todo_array = [ "Buy Milk", "Buy Soap", "Pay bill", "Draw Money" ]
end
end
Here's my view:
<%= #todo_array.each do |t| %>
<%= puts t %><\br>
<% end %>
Here's the result:
<\br> <\br> <\br> <\br> ["Buy Milk", "Buy Soap", "Pay bill", "Draw Money"]
Erb, the templating engine you're using in your views, has a few different ways of embedding ruby code inside templates.
When you put code inside <%= %> blocks, erb evaluates the code inside and prints the value of the last statement in the HTML. Since .each in ruby returns the collection you iterated over, the loop using <%= %> attempts to print a string representation of the entire array.
When you put code inside <% %> blocks, erb just evaluates the code, not printing anything. This allows you to do conditional statements, loops, or modify variables in the view.
You can also remove the puts from puts t. Erb knows to try to convert the last value it saw inside <%= %> into a string for display.
Hey why are you putting '=' sign in first line. <% %> are used for telling rails that string under this is ruby code,evaluate it. Where as <%= %> this tells rails that string in these tags is in ruby, evaluate it and print the result in html file too.
Hence try to inspect your code you are writing
<%= #todo_array.each do |t| %>
while this line is only for iterating over #todo_array hence we wont be in need to print that line. So final code should be
<% #todo_array.each do |t| %>
<%= puts t %>
<% end %>
Just try:
<%= #todo_array.join('<br />').html_safe %>
instead of
<%= #todo_array.each do |t| %>
<%= puts t %><\br>
<% end %>
Two issues with your view:
you are using "<%=" where you should be using "<%".
you don't need 'puts'
This should improve your results:
<% #todo_array.each do |t| %>
<%= t %><\br>
<% end %>
I would further consider using some HTML structure to better structure your todo list (instead of using br tag at the end of lines), perhaps an un-ordered list like so:
<ul>
<% #todo_array.each do |t| %>
<li><%= t %></li>
<% end %>
</ul>
Remove the equal sign from your each statement:
<% #todo_array.each do |t| %>
<%= t %><\br>
<% end %>
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| %>