How to print an HTML table using Ruby? - ruby

This is my code:
<table class="video_table">
<% count = 0 %>
<tr>
<% #f_videos.each do |f_video| %>
<td><%= f_video.name %></td>
<td><%= f_video.date_added %></td>
<td><%= f_video.views %></td>
<% count +=1 %>
<% if count == 4 %>
</tr>
<% end %>
<% end %>
</table>
at each 4 videos placed I want the table to switch row. So I implemented a counter.
But it is not working. Any ideas?

Your count will only be set to 4 once.
Instead of if count == 4 use if count % 4 == 0
This will repeat the </tr> for each multiple of 4
Alternatively, you could skip using the count variable and use each_with_index to get the same result
<table class="video_table">
<% #f_videos.each_with_index do |f_video, i| %>
<tr>
<td><%= f_video.name %></td>
<td><%= f_video.date_added %></td>
<td><%= f_video.views %></td>
<% if (i+1) % 4 == 0 %>
</tr>
<% end %>
<% end %>
</table>
Even better! each_slice
<table class="video_table">
<% #f_videos.each_slice(4).to_a do |slice| %>
<tr>
<% slice.each do |f_video| %>
<td><%= f_video.name %></td>
<td><%= f_video.date_added %></td>
<td><%= f_video.views %></td>
<% end %>
</tr>
<% end %>
</table>

Another solution:
<table class="video_table">
<% #f_videos.in_groups_of(4) do |group| %>
<tr>
<% group.each do |f_video| %>
<td><%= f_video.name %></td>
<td><%= f_video.date_added %></td>
<td><%= f_video.views %></td>
<% end %>
</tr>
<% end %>
</table>
in_groups_of has the added value/advantage that, when needed, it allows for padding any remaining slots with eg ' ', which can be very useful. See the docs.

Related

not able to print date in Month_name-yyyy format in rails

hi I am taking date input in mm-yyyy format I want to print date in format of month_name-yyyy format. i have used strftime but i am not to able print the date as my required format.
index.html.erb code
<td><%= project_site.name.titleize %></td>
<td><%= project_site.created_at.strftime('%b-%Y') %></td>
<td><%= link_to ' View attendance', project_site.file, :class => "fi-page-export-csv" %></td>
<% project_site.manager_remarks.each do |manager_remark| %>
<% if manager_remark.decision == false %>
<td><%= 'Rejected' %></td>
<% elsif manager_remark.decision == true %>
<td><%= "Approved" %></td>
<% else %>
<td><%= "Pending" %>
<% end %>
<% end %>
<td><%= project_site.attendance_month.strftime('%b %Y') %></td>
<td><%= link_to 'Remark ', project_site %><span>(<%= project_site.manager_remarks.size %>)</span></td>
<td><%= link_to 'Edit', edit_project_site_path(project_site) %></td>
<td><%= link_to 'Delete', project_site, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
form.html.erb code
<%= form.label :name %>
<%= form.text_field :name %>
</div>
<!--
<div class="field medium-3 columns">
<%= form.label :date %>
<%= form.text_field :date, class: 'datepicker' %>
</div>
-->
<div class="field medium-3 columns">
<%= form.label :upload_attendance %>
<%= form.file_field :file, :class=> 'attendance-file' %>
</div>
<div class="field medium-6 columns">
<%= form.label :attendance_month %>
<%= form.date_select :attendance_month, { :discard_day => true, :discard_month => false, :discard_year => false },:class => 'datetime' %>
</div>
https://www.foragoodstrftime.com/ should help you out
Try:
<td><%= project_site.attendance_month.strftime('%B %Y') %></td>
%B denotes to full name of month in strftime.
Try this code snippet.
<td><%= project_site.created_at.strftime('%B-%Y') %></td>

how to disable checkbox on condition?

I am trying to create a spreadsheet on index page to display items with checkbox and if validated - the checkbox will be disabled so that people won't mis-ticked it.I was wondering should i do it with controller or the view? thanks : can you help?
In app/views/scooties_coupons/index.html.erb
<table class="table table-hover">
<h1>Scooties Coupons</h1>
<%= form_with(url: validate_coupons_path, method: 'patch') do |f| %>
<table>
<thead>
<tr>
<th>Valid</th>
<th>Coupon</th>
<th>Redeemed</th>
<th>First name</th>
<th>Surname</th>
<th>email</th>
<th>occupation</th>
<th>validation</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #scooties_coupons.each do |scooties_coupon| %>
<tr>
<td>
<%= fields_for('scooties_coupons[]', scooties_coupon) do |cf|
cf.check_box(:validated)
end %>
</td>
<td><%= scooties_coupon.coupon %></td>
<td><%= scooties_coupon.redeemed %></td>
<td><%= scooties_coupon.first_name %></td>
<td><%= scooties_coupon.surname %></td>
<td><%= scooties_coupon.email %></td>
<td><%= scooties_coupon.occupation %></td>
</tr>
<% end %>
</tbody>
</table>
<%= f.submit %>
<% end %>
<br>
</table>
in app/controllers/scotties_coupons_controller.rb:
def set_valid_coupons
to_valid = params[:scooties_coupons].select do |id, attrs|
attrs[:validated] == '1'
end
to_not_valid = params[:scooties_coupons].reject do |id, attrs|
attrs[:validated] == '1'
end
ScootiesCoupon.transaction do
ScootiesCoupon.where(id: to_valid.keys, validated: false).update_all(
validated:true)
ScootiesCoupon.where(id: to_not_valid.keys, validated: true).update_all(
validated:false)
end
redirect_to action: :index, notice: 'Validations updated'
end
you can do something like
<table class="table table-hover">
<h1>Scooties Coupons</h1>
<%= form_with(url: validate_coupons_path, method: 'patch') do |f| %>
<table>
<thead>
<tr>
<th>Valid</th>
<th>Coupon</th>
<th>Redeemed</th>
<th>First name</th>
<th>Surname</th>
<th>email</th>
<th>occupation</th>
<th>validation</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #scooties_coupons.each do |scooties_coupon| %>
<tr>
<td>
<% if true %>
SOMETHING HERE
<% else %>
<%= fields_for('scooties_coupons[]', scooties_coupon) do |cf|
cf.check_box(:validated)
end %>
<% end %>
</td>
<td><%= scooties_coupon.coupon %></td>
<td><%= scooties_coupon.redeemed %></td>
<td><%= scooties_coupon.first_name %></td>
<td><%= scooties_coupon.surname %></td>
<td><%= scooties_coupon.email %></td>
<td><%= scooties_coupon.occupation %></td>
</tr>
<% end %>
</tbody>
</table>
<%= f.submit %>
<% end %>
<br>
</table>

I need to calculate the grand total of each row in my Cost table in rails 4

In my costs table I have created a variable to calculate the rate multiplied by the hours and I can obtain my figure successfully. However, I need to calculate the total of each row at the end of my table. One of my fields are "total of all rows". What is the best course of action for this?
Any suggestions?
In my costs/index.html.erb file this is the code I currently have
<tbody>
<% #costs.each do |cost| %>
<tr>
<td><%= cost.mini_description %></td>
<td><%= cost.description %></td>
<td><%= cost.quantity %></td>
<td><%= cost.rate %></td>
<td><%= cost.total%><%=cost.cost_var%></td>
<td><%= cost.total_of_all_rows %></td>
<!--<td><%#= cost.job %></td>-->
<td><%= link_to 'Show', cost %></td>
<td><%= link_to 'Edit', edit_cost_path(cost) %></td>
<td><%= link_to 'Destroy', cost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
<thead>
<tr>
<th>Total of all rows</th>
</tr>
</thead>
I have fixed my code yet I am still not getting the desired result my total_of_all_rows is not adding up the sum of rate and quantity. My amended code is:
<tbody>
<% #costs.each do |cost| %>
<tr>
<td><%= cost.mini_description %></td>
<td><%= cost.description %></td>
<td><%= cost.quantity %></td>
<td><%= cost.rate %></td>
<td><%= cost.total%><%=cost.cost_var%></td>
<!--<td><%#= cost.job %></td>-->
<td><%= link_to 'Show', cost %></td>
<td><%= link_to 'Edit', edit_cost_path(cost) %></td>
<td><%= link_to 'Destroy', cost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
</tbody>
<thead>
<tr>
<th>Total of all rows</th>
</tr>
</thead>
<tr>
<td><%= cost.total_of_all_rows %><%=Cost.sum(:total) %></td>
</tr>
<% end %>
<tbody>
</tbody>
</table>
My migration for this table is
class CreateCosts < ActiveRecord::Migration
def change
create_table :costs do |t|
t.string :mini_description
t.string :description
t.string :quantity
t.string :rate
t.string :total
t.string :total_of_all_rows
t.references :job, index: true
t.timestamps
end
end
end
1)Don't store the total_of_all_rows in the databse as long as it is variable and you will have to update it with each new entry.
2)Delete column from migration.
3)In your view:
<tbody>
<% #costs.each do |cost| %>
<tr>
<td><%= cost.mini_description %></td>
<td><%= cost.description %></td>
<td><%= cost.quantity %></td>
<td><%= cost.rate %></td>
<td><%= cost.total%><%=cost.cost_var%></td>
<!--<td><%#= cost.job %></td>-->
<td><%= link_to 'Show', cost %></td>
<td><%= link_to 'Edit', edit_cost_path(cost) %></td>
<td><%= link_to 'Destroy', cost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
</tbody>
<thead>
<tr>
<th>Total of all rows</th>
</tr>
</thead>
<tr>
<td><%= #costs.sum(:total) %></td>
</tr>
<% end %>
<tbody>
If you still want to store the value:
class Cost < ActiveRecord::Base
after_save :update_totals
def update_totals
Item.all.each {|x| x.total_of_all_rows = Item.sum(:total)
end
end

How to work with string?

I have a string with emails (name, surname, email):
#emails = "Nina Beeu luda#hotmail.com, Vasilina Korute valaj#kos.co.uk, Hikote Ewefs valaj#kos.co.uk,
Egert Erm papa#sasee.ee, Sambuka Ioas valaj#kprivet.com, Vanish Kiki sasa#sas.com, Inoke Xxx saop#hot.ee"
I need to substring from this string: name, surname and email and paste them into table:
<table border=1>
<tr>
<td>
Name
</td>
<td>
Surname
</td>
<td>
Email
</td>
</tr>
</table>
How i can do it?
<table>
<% #emails.split(", ").each do |chunk| %>
<tr>
<% ["Name", "Surname", "Email"].zip(chunk.split(" ")).each do |data| %>
<td><%= data.join(": ")</td>
<% end %>
</tr>
<% end %>
</table>
#emails.split(/,\s+/).each do |details|
name, surname, email = details.split(" ")
# do your html creaty thing here
end
More explicitly, you could do this in erb:
<table border=1>
<% #emails.split(/,\s+/).each do |details| %>
<% name, surname, email = details.split(/\s+/) %>
<tr>
<td><%= name %></td>
<td><%= surname %></td>
<td><%= email %></td>
</tr>
<% end %>
</table>
And a variant in haml:
%table(border=1)
- #emails.split(/,\s+/).each do |details|
%tr
- details.split(/\s+/) do |detail|
%td= detail

How to print SUM, and AVG SQL values with JDBC?

Okay, I am trying to do print the results of a SUM and AVG function using JDBC. So basically, it's not working. What am I doing wrong?
<table border="1">
<tr><th>Total Homes Sold</th><th>Total Sales Amount</th><th>Averages Price Per Home</th></tr>
<% rset = stmt.executeQuery("SELECT COUNT(home_ID) FROM home");%>
<% rset1 = stmt.executeQuery("SELECT SUM(purchase_Price) FROM home");%>
<% rset2 = stmt.executeQuery("SELECT AVG(purchase_Price) FROM home");%>
<tr>
<td><%= rset.getString(1) %></td>
<td><%= rset1.getString(1) %></td>
<td><%= rset2.getString(1) %></td>
</tr>
</table>
The main problem is that you need to call next() on the result set (after executeQuery()) to retrieve the first row of the result set (even though there's just one row).
It you don't do it, no row is available and calling getString() causes the "ResultSet is closed" error.
Try doing:
<tr>
<% rset = stmt.executeQuery("SELECT COUNT(home_ID) FROM home");%>
<td><%= rset.getString(1) %></td>
<% rset = stmt.executeQuery("SELECT SUM(purchase_Price) FROM home");%>
<td><%= rset.getString(1) %></td>
<% rset = stmt.executeQuery("SELECT AVG(purchase_Price) FROM home");%>
<td><%= rset.getString(1) %></td>
</tr>
Or better:
<tr>
<% rset = stmt.executeQuery("SELECT COUNT(home_ID), SUM(purchase_Price), AVG(purchase_Price) FROM home");%>
<td><%= rset.getString(1) %></td>
<td><%= rset.getString(2) %></td>
<td><%= rset.getString(3) %></td>
</tr>

Resources