String iteration using each_line - ruby-on-rails

I have the following code which displays instructions in an ordered list. When it's rendered in the browser, it outputs all of the instructions at the end in a single line. Am I doing something wrong? See output below for example.
Code:
<% #recipe.instructions.each_line do |instruction| %>
<li><%= instruction %></li>
<% end %>
Output:
<p>
<b>Instructions:</b>
<ol>
<li>Roast garlic
</li>
<li>test
</li>
<li>eat icecream</li>
Roast garlic
test
eat icecream </ol>
</p>
Rails 3 rc2 ruby 1.9.2dev (2010-07-11 revision 28618)

Are you sure you aren't doing something like this instead?
<%= #recipe.instructions.each_line do |instruction| %>
<li><%= instruction %></li>
<% end %>
Note the extra = at the beginning of the loop. Since each_line returns the string, it'll spit the string out again.

So I finally figured this out. I changed the code to use Array#each and removed the equal sign in the block helper.
Final code:
<% #recipe.instructions.split(/\r\n/).each do |instruction| %>
<li><%= instruction %></li>
<% end %>

Related

Instagram gem returns full hash after iterating through it

I am using the instagram gem with rails. My problem is I am iterating over all of the data in the response with code like this:
<div>
<ul>
<%= #instagram.each do |pic| %>
<li><%= pic.name %></li>
<li><%= pic.latitude%></li>
<li><%= pic.longitude%></li>
<% end %>
</ul>
</div>
I am getting the desired results except for the end, where this gets printed in the browser:
[#<Hashie::Mash id="152201866" latitude=45.89172 longitude=-64.370013 name="Marshview Middle School">, ... etc...]
Any help trying to figure out how to ignore that last bit would be greatly appreciated.
Change
<%= #instagram.each do |pic| %>
to
<% #instagram.each do |pic| %>
= expects a return and prints the collection it iterates over.

Getting extra information when looping in Ruby

I'm using the each do loop correctly, and not getting errors when looping an active record base. But for some reason, I am getting extra information at the end.
Here's what my controller looks like:
def archivedBlogs
#compsci = Compsci.all
#personalb = Personalb.all
end
And here is the code I have in the view page:
<div class="panel">
<ul>
<%= #compsci.each do |blog| %>
<li><%= blog.title %></li>
<% end %>
</ul>
</div>
But as you can see, I'm getting extra stuff at the end:
How can I fix it so that it only prints the blog titles?
Change
<%= #compsci.each do |blog| %>
to
<% #compsci.each do |blog| %>

get specific element from ruby block, rails app

When performing a block like:
<% #user.favoured_user.each do |user| %>
<li><%= user.name %></li>
<% end %>
With the favoured_user method returning a limit of 5 users, how would I manipulate the block so that even when there are only 3 users available, I could still return 5 li elements?
I'm guessing a helper would come in to play, and maybe the 'first, second, third, etc.' array methods, but I can't think how to write it.
Any help?
You can try this,
<% 5.times do |i| %>
<li> <%= #user.favoured_user[i].try(:name) %> </li>
<% end %>
You can use in_groups_of
Like:
<% #user.favoured_user.in_groups_of(5).each do |favored_user| %>
<% favored_user.each do |user| %>
<li><%= user.try(:name) %></li>
<% end %>
<% end %>
The first 3 users will come through, and the last two entries will be nil

How to show app data in erb template?

A first look I thought erb accepts any Ruby code, but I've got this strange behaviour...
I have an array [of tags for my article], and I want to make a nice display for them. So I'm writing something like this:
<ul>
<% #post.tags.each do |item| %>
<li>item</li>
<% end %>
</ul>
The wrong output looks like this:
<ul>
<li>item</li>
<li>item</li>
<li>item</li>
...
</ul>
Where I am wrong? Any suggestions how to make a proper iteration?
You forgot the <%= %> to display the value of item:
<ul>
<% #post.tags.each do |item| %>
<li><%= item %></li>
<% end %>
</ul>

When do you use <% -%> instead of <% %>

I've noticed that in some lines of rails views, this is used:
<% # Code... -%>
instead of:
<% # Code... %>
What is the difference?
<ul>
<% #posts.each do |post| -%>
<li><%=post.title%></li>
<% end -%>
</ul>
There will be no new lines in between the <ul> and first <li> and the last closing </li> and </ul>. If the - was omitted, there would.
The different options for evaluating code in ERB are as follows (they can be accessed in Textmate using Ctrl-Shift-. ):
<% %> Just evaluate the contents.
<%= %> Evaluate the contents and puts the result.
<%= -%> Evaluate the contents and prints the result.
<%# %> The contents is treated as a comment and not outputted.
Notice the difference between puts and print. Puts always adds a new line at the end of the string whereas print doesnt.
Basically, the -%> says don't output a new line at the end.
Consider this
<div>
<% if #some_var == some_value %>
<p>Some message</p>
<% end %>
</div>
The code above yields to the HTML below if the #some_var is some_value
<div>
<p>Some message</p>
</div>
If you've put - in the closing tag, then the ERB interpreter would remove the new lines for those with code tag including - and result in the following
<div>
<p>Some message</p>
</div>
This is useful if you need to have a good looking code for HTML. Sometimes you'll find it useful when working sideby side with a designer
Hope this helps.
A little late, but I think it's worth pointing out that you can also do this:
<%- #posts.each do |post| -%>
<li><%= post.title %></li>
<%- end %>
This strips away any whitespace in front.

Resources