I have a hash of images that i'd like to output via a html table.
It's a basic task that I've done in PHP but I'd like to see what magical stuff ruby can do to achieve it easily.
I've looked at helpers and tutorials and they're all very complicated.
Here's how I am currently printing the images.. How would I convert it to output to something like a 4 x n table?
<% #photos.each do |photo| %>
<%= image_tag photo["images"][4]["source"] %><br/>
<% end %>
Take a look at the each_slice method, which lets you grab your array in groups of a given size. You should be able to do something like
<table>
<% #photos.each_slice(4) do |group| %>
<tr>
<%group.each do |photo|%>
<td>
<%= image_tag photo["images"][4]["source"] %>
<td>
<% end %>
</tr>
<% end %>
</table>
I think in_groups_of might help you out:
<table>
<% #photos.in_groups_of(4, false) do |photos| %>
<tr>
<% photos.each do |photo| %>
<td><%= image_tag photo["images"][4]["source"] %></td>
<% end %>
</tr>
<% end %>
</table>
By passing false as the second argument, the last row might be incomplete. If you want to show a blank image instead, you can pass an empty photo object (assuming its URI points to the blank image your website is using) or simply the direct URI, if the photos array is only an array of URIs.
Related
I have an issue in the node (html file). I need to get the first data instead of everything. I used this
<% if(tblData) {%>
<% tblData.forEach(function(res,row) {%>
<tr>
<td>
<%= res.name %>
</td>
</tr>
<% }) %>
<% } %>
I got all the names. But instead of getting all the names I want only the first name. Is there any solution for this?
You can get the first element of a table by putting its index in square brackets (myTable[0]):
<% if(tblData) {%>
<tr>
<td>
<%= tblData[0].name %>
</td>
</tr>
<% } %>
I'm trying to make my own site and I don't know how to better dispay images from database. I want to display six images in one row, but the next six item I want to display in second row. How can I make it with this code?
<table width="80%">
<% #products.each do |product| %>
<td>
<%= product.price %>"> <%= image_tag(product.image_url) %> </td>
<% end %>
</table>
Sorry, if this question is really stupid. But I'm just learning and It will help me understand Ruby better.
You could use Enumerable#each_slice to divide them into sub arrays like so:
<table width="80%">
<% #products.each_slice(6) do |row| %>
<tr>
<% row.each do |product| %>
<td><%= product.price %>"> <%= image_tag(product.image_url) %> </td>
<% end %>
</tr>
<% end %>
</table>
I'm currently scraping product information from a website and am attempting to output it into a table on a webpage.
I'm stuck because the only code that I can get to properly format as
ITEM
IMAGE
PRICE
Is clearly wrong, and ends up replicating the data many times.
The partially functioning code is:
<table>
<tr>
<% #items.each do |title| %>
<% #images.each do |image| %>
<% #prices.each do |price| %>
<br>
<td><%= title %></td>
<br>
<td><%= image %></td>
<br>
<td><%= price %></td></tr>
<br>
</table>
<% end %>
<% end %>
<% end %>
I've tried formatting the code like below, but it tends to drop the three sets of data down like
TITLE TITLE TITLE IMAGE IMAGE IMAGE PRICE PRICE PRICE.
in a variety of very incorrect ways.
<table>
<tr>
<td>
<% #items.each do |title| %>
<%= title %>
<% end %>
</td>
<br>
<td>
<% #images.each do |image| %>
<%= image %>
<% end %>
<br>
<td>
<% #prices.each do |price| %>
<%= price %>
<% end %>
</td>
<br>
</table>
Is anyone able to spot the mistake I'm making or know what I need to do to properly output this code? Do I need to show you any more code?
If you want to iterate over the three arrays together you need to zip them. This will enable you to render each triplet together (the first item with the first image and the first price; the second item with the second image and the second price; etc.):
<table>
<% #items.zip(#images, #prices).each do |title, image, price| %>
<tr>
<td><%= title %></td>
<td><%= image %></td>
<td><%= price %></td>
</tr>
<% end %>
</table>
I'm passing the results of a multi-select box to a page so that the selections can be shown on screen. As it's multi-select, the result can either be a scalar or an array reference. Is there a way of finding this out? I can't find anything online, but I thought there might be a .array or .array_ref token that could be used for validation.
I'm using Template Toolkit, Perl and Dancer.
So here is what I've got for a scalar:
<% IF multitext %>
Text: <% multitext %>
<% END %>
What I want is something like...
<% IF multitext %>
<% IF multitext.array_ref %> <!-- whatever works! -->
<% FOREACH text IN multitext %>
Text: <% text %>
<% END %>
<% ELSE %>
Text: <% multitext %>
<% END %>
<% END %>
If <%- multitext.0 -%> returns a non-zero value, it's an arrayref.
If <%- multitext.keys.size -%> returns a non-zero value, it's a hashref.
The way I usually handle it is to force it to be an array if it's a scalar, eg:
<%- SET items = multitext.0 ? multitext : [ multitext ];
FOREACH item IN items;
...
END; -%>
Several years later...
You could use the .list vmethod to guarantee it's an array e.g.
<% FOREACH text IN multitext.list %>
Text: <% text %>
<% END %>
See http://template-toolkit.org/docs/manual/VMethods.html#section_list
I have created a custom method that will return unique items, together with the number of occurrences in a table. I have the following code:
#answers = Answer.all.count(:group => 'answer')
The result is fine when rendering it to JSON, showing:
{"0,0":1,"1,2":1,"2,2":1,"3,3":1}
...but I am unable to render the result to html. I can not display the key, values:
<% #answers.each do |answer| %>
<tr>
<td>key: <%= #answer.??? %></td>
<td>value: <%= #answer.??? %></td>
</tr>
<% end %>
If answers is a hash (and it looks like it is) you can pass the key and value into the rendering block by using two parameters instead of one.
like this:
<% #answers.each do |key, val| %>
<tr>
<td>key: <%= key %></td>
<td>value: <%= val %></td>
</tr>
<% end %>