How to show app data in erb template? - ruby-on-rails

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>

Related

Render an attributes of each object - Rails

I have a view right now that renders an object on the page. The object is an Integration. On the Integration object I have attribute called filters. Filters are stored as an array. All I need to do is list out the filters of each integration below them in a list. Here is my code.
View
<% if #integrations.any? %>
<div class="configured-integrations">
<h3 class="heading-3">My Configured Integrations:</h3>
<ul class="integration-list integration-list--compact">
<%= render #integrations %>
</ul>
</div>
<% end %>
Screenshot
In the screenshot you can see that each of those elements are integrations. I need to list the filters of each integration below the title there.
Controller
def index
# Get the list of the user's integrations grouped first by provider then
# from oldest to newest."
#integrations = current_account.integrations
.order(type: :asc, created_at: :asc)
end
I hope this is clear enough. So recap: I need to list the filters on each integration below. I've already tried stuff like this #integrations.first.filters but that wont work because it's a static call. I need something like a list. Thank you
You can add another partial to render all filters which are associated with your Integration.
Create a partial file _show_filters.html.erb in your views
<% filters.each do |filter| %>
<li><%= filter %></li>
<% end %>
And render this partial while iterating through your #integration object like this.
<% if #integrations.any? %>
<div class="configured-integrations">
<h3 class="heading-3">My Configured Integrations:</h3>
<ul class="integration-list integration-list--compact">
<% #integrations.each do |integration| %>
<li>
<%= integration %>
<ul class="">
<%= render 'show_filters', filters: integration.filters %>
</ul>
</li>
<% end %>
</ul>
</div>
<% end %>
What you need to iterate through each integration, then <%= render integeration.filters %>
<% if #integrations.any? %>
<div class="configured-integrations">
<h3 class="heading-3">My Configured Integrations:</h3>
<ul class="integration-list integration-list--compact">
<% #integrations.each do |integration| %>
<li>
<%= integration %>
<ul class="">
<%= render integration.filters %>
</ul>
</li>
<% end %>
</ul>
</div>
<% end %>
You will have to update this code to make the partials work, but i hope this gets the idea across.
You can't use the shortcut <%= render #integrations %> here, because you want a subgroup inside #integrations. So you'll have to do it the long way.

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| %>

Why Rails images display #<Photo:0xB4F28FA># object id?

Here is my view:
<div>
<ul>
<%= #album.photos.each do |photo| %>
<li><%= link_to(image_tag(photo.soure.url(:small)),photo.source.url(:medium)) %></li>
<% end %>
</ul>
</div>
produces the right result except all the object ids (i.e. #<Photo:0xXXXXXX>#) get added right before the </ul> and display in the html. I'm guessing since each time the block gets executed it returns the Photo object and that's why its rendering all the #<Photo:0x>s but i don't know how to STOP this from happening.
It's because you have:
<%= #album.photos.each do |photo| %>
instead of:
<% #album.photos.each do |photo| %>

String iteration using each_line

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 %>

Resources