Rails 3: retrieving different attribute depending on loop count? - ruby-on-rails

I have a loop in one of my views to display a table like so:
Each category object has 5 attributes called: level_1, level_2, level_3, level_4, level_5.
There will be an array with all the category objects. so there could be any amount of categories and no necessarily 3.
what would the best way to draw this up be? i have something like this at the moment but dont know how to select the relevant category level attribute in the 5.times loop.
<table border="0">
<tr>
<th>Maturity</th>
<% for category in #categories %>
<th><%= category.category_title %></th>
<% end %>
</tr>
<% 5.times do |i|%>
<% level = 5 - i %>
<tr>
<td>Level <%= level %> Maturity</td>
<% for category in #categories %>
<td><%= category.level_ #put in the level value here so it selects the relevant attraibute %></td>
<% end %>
</tr>
<% end %>
</table>

you need to change the rendering of level with this:
<% for category in #categories %>
<td><%= category.send("level_#{level}") %></td>
<% end %>
send is used to call a method on an object so that you can compose your method at runtime.

If you categories as variable no. then you shouldn't make it columns, but rows. And then levels will be you columns e.g.
<table>
<tr>
<th>Level 1</th>
<th>Level 2</th>
<th>Level 3</th>
<th>Level 4</th>
<th>Level 5</th>
<tr>
<% #category.each do |c| %>
<tr>
<td>#category.level_1<td>
<td>#category.level_2<td>
<td>#category.level_3<td>
<td>#category.level_4<td>
<td>#category.level_5<td>
<th>
<% end %>
Now in above code you may replace hard coded level_#{no} with iterations.

Related

Hide table header if there is no column attribute - Ruby on rails

Here is a index.html.erb. How do I hide the table header, such as Remark, with a method when no records exists in remark attribute column? Preferably without using JavaScript.
index.html.erb
<table id = "kola" class="table listing text-center">
<% has_remark = collection_has_remark?(#aslani361s) %>
<thead>
<tr class="tr-head">
<td>Date</td>
<td>Description</td>
<td>Amount</td>
<td>Discount</td>
<td>Paid</td>
<td>Balance</td>
<td>DelnDel</td>
<% if has_remark %>
<td>Remark</td>
<% end %>
<td>Hide</td>
</tr>
</thead>
</table>
However I am able to hide the remark attribute values like as below ;
_aslani361.html.erb
<% if aslani361.remark.present? -%>
<td class="col-1"><%= aslani361.remark %></td>
<% end %>
aslani361s_helper.rb
module Aslani361sHelper
def collection_has_remark?(collection)
collection.each do |aslani361|
if aslani361.remark.present?
return true
end
end
end
end
aslani361.rb
class Aslani361 < ActiveRecord::Base
end
Any suggestions are most welcome.
Thank you in advance.
If you wish to hide the column because no records in your array has the remark value, you can do something like this:
Define a method in your helper module file for the controller:
def collection_has_remark?(collection)
collection.each do |record|
if record.remark.preset?
return true
end
end
end
Then use it in the view
<% has_remark = collection_has_remark?(#records) %>
<thead>
<tr class="tr-head">
<td>Date</td>
<td>Description</td>
<td>Amount</td>
<td>Discount</td>
<td>Paid</td>
<td>Balance</td>
<% if has_remark %>
<td>Remark</td>
<% end %>
</tr>
</thead>
Then use the same if statement inside your loop. Personally I think it's important to leave an empty column, so users know for sure it doesn't have one.

Rails view: shuffle table column

I am having trouble figuring out how to shuffle table values in a view. I have a table in my view with a left and a right column, and would like to shuffle only the right column.
show.html.erb
<table>
<% #items.each do |item| %>
<tr>
<td><%= item.left %><td>
<td><%= item.right %><td>
</tr>
<% end %>
</table>
The "left" and "right" share the same primary id in the database. Any suggestions about how to shuffle only one side? Thanks!
You can use shuffle, do this way
<% shuffled_items = #items.shuffle %>
<% #items.each_with_index do |item, index| %>
<tr>
<td><%= item.left %><td>
<td><%= shuffled_items[index].right %><td>
</tr>
<% end %>
For details read this documentation http://ruby-doc.org/core-1.9.3/Array.html#method-i-shuffle
I think the simplest way is to have 2 arrays.
#items_left and #items_right
eg:
items = Item.a_scope
#items_left = items
#items_right = items.pluck(:right).shuffle #if you are on > rails 3.2
# #items_left = items.pluck(:left) #if only that attribute is needed
so you can use it as follows
<table>
<% #items_left.each_with_index do |item, i| %>
<tr>
<td><%= item.left %><td>
<td><%= #items_right[i] %><td>
</tr>
<% end %>
</table>

HTML Table issue grabbing data from two database tables

Iam having an issue getting data from two different database tables to display properly with in an HTML table. I'm sure I am just overlooking something and this is pretty easy, but for whatever reason I cannot get quantity to display, or the <td class="sub-total"> to display either.
Demands (you can think of these as orders) and items are connected through a table called demand_items. The demand_items table also has the quantity of the item ordered. The page will display but the quantity and the subtotal will not render.
Here is the html:
<h3>Order for <%= #demand.customer.name %></h3>
<h4>Ordered for: <%= #demand.date %></h4>
<h4>Add Items</h4>
<%= render 'demands/item_form' %>
<table class="customer-table">
<tr>
<th>Item</th>
<th>Price</th>
<th>Quantity</th>
<th>Sub Total</th>
</tr>
<% #demand.items.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.price %></td>
<% #demand.demand_items do |quantity| %>
<td><%= quantity.quantity %></td>
<td class="sub-total"><%= (item.price) * (quantity.quantity) %></td>
<% end %>
<% end %>
</tr>
</table>
#demand.demand_items.each do |quantity|..........instead of #demand.demand_items do |quantity|......

Rails creating a thumbnail gallery in my view

I currently have an Array that contains some URL's to images.
#images = [ "http://site/images/01.jpg", "http://site/images/02.jpg" ]
Total 18 images
I would like to take this array and create a thumbnail gallery where the gallery is 3 columns across in my view. The HTML out put would be
<table>
<tr>
<td><img src="http://site/images/01.jpg"></td>
<td><img src="http://site/images/02.jpg"></td>
<td><img src="http://site/images/03.jpg"></td>
</tr>
<tr>
<td><img src="http://site/images/04.jpg"></td>
<td><img src="http://site/images/05.jpg"></td>
<td><img src="http://site/images/06.jpg"></td>
</tr>
</table>
My current implementation gets me a one column table
<table>
<tr>
<% #images.each do | image | %>
<td><%= image_tag(image)%></td><br>
<% end %>
</tr>
</table>
In the future I might want it to be 6 columns instead of 3 columns.
I'm looking for a way to do this in a clean and flexible way.
I was looking at the Ruby documentation and I saw this
Class Range (rng.step method)
http://www.ruby-doc.org/core/classes/Range.html
Not sure if this Range class step method can solve the problem but the example it provides is interesting.
Any thoughts, I'm still learning and maybe I'm over thinking this?
use each_slice()
<table>
<% #images.each_slice(3) do |images| %>
<tr>
<% images.each do |image| %>
<td><%= image_tag(image) %></td>
<% end %>
</tr>
<% end %>
</table>
Untested. You need to use each_with_index and the modulus % operator.
<% #images.each_with_index | image, index | %>
<% unless index % column_count == 0 %>
<td><%= image_tag(image)%></td>
<% else %>
<td><%= image_tag(image) %></td>
</tr>
<tr>
<% end %>
<% end %>

How to add values from table columns?

Hi i have a table where i list different values. Now i want to add a total values to my table that counts the values from each column and adds it to a column with the total value. How do I do this? And is it possible to do this in a simple way in the view, or am I bound to do it in the model? Thanks for any help!
<div class="chart-data">
<table>
<caption>Data</caption>
<thead>
<tr>
<% statistic.column_titles.each do |column| %>
<th><%= column %></th>
<% end %>
<th>Total</th>
</tr>
</thead>
<tbody>
<% statistic.rows.each do |row| %>
<tr>
<th scope="row"><%= row.title %></th>
<% row.data.each do |column| %>
<td><%= column %></td>
<% end %>
</tr>
<------- Here i want to have the code that sums the values from the columns above to form the 'Total'
<% end %>
</tbody>
</table>
</div>
The simple solution would be to maintain a variable that sums up the column, then use it for the total at the end.
class Row < ActiveRecord::Base
def total_spent
sum(:data)
end
end
<tbody>
<% #rows.each do |row| %>
<tr>
<th scope="row"><%= row.title %></th>
<% row.data.each do |column| %>
<td><%= column %></td>
<% end %>
</tr>
<td><% row.total_spent %></td>
<% end %>
</tbody>
Assuming of course that row is a model you could move this logic inside the row model. You may cache the page as well if you like.
If your data is the result of an ActiveRecord query against a database you could ask the database to do the work of summing all the columns. It would be one additional query but would run very quickly as that's something databases are designed to do.

Resources