How to put the average in the last table in Rails - ruby-on-rails

this my code and it increases always when i have an evaluation, in the end it shows the grade, but, the average always stays after the grade. I wanted to know how I do it to put it at the end.
<% #registration.matrix.blocks.each do |block| %>
<h5>Block: <%= block.description %></h5>
<div class="table-responsive">
<table class="table table-sm table-striped table-bordered shadow-sm">
<thead class="thead-dark">
<tr>
<th>Discipline</th>
<th>Assigned Fouls</th>
<th>Test 1</th>
<th>Test 2</th>
<th>Test 3</th>
<th>Test 4</th>
<th>Test 5</th>
<th>Test 6</th>
<th>Test 7</th>
<th>Test 8</th>
<th>Final Test</th>
<th>Average</th>
</tr>
</thead>
<tbody>
<% block.disciplines.distinct.each do |discipline| %>
<tr>
<td><%= discipline.name %></td>
<td>
<%= number_of_absences(current_user, discipline) %>
</td>
<% discipline.evaluations.each do |evaluation| %>
<td><%= Evaluate.get_evaluate(current_user.id, evaluation.id).present? ? Evaluate.get_evaluate(current_user.id, evaluation.id).note : '0,0' %></td>
<% end %>
<td><%= get_media(current_user, discipline) %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
</div>
Image to see the bug
So I would like to know how I fix the error, for example I put the grade 10, it divides by 8 as I programmed, but the average that is 10/8 since 10 was the only grade that assigns, it gets 1,25 however the average is on the side and not in the end, as I do to put in the end in the table "average"

If the question is how to align the average value under the Average table header? the answer is that you have to put as many td as needed in order to align it there, otherwise I didn't understand the question :-)

Related

Rails mapping by max size and loop

In my rails application I'm looking to display in a table some results mapping by max size.
Controller
#max_length = [#result.generals, #result.measurements.in_groups_of(3)].map(&:size).max
The problem i'm facing is my #result.measurement has 3 dates for each measurement in which I want to display across the row.
This is what I'm looking to achieve:
But with the code below, this is my result - It stops at the first date:
How can I go about looping the #result.measurements?
<table>
<tr>
<th colspan="2">General</th>
<th colspan="6">Measurements</th>
</tr>
<tr>
<td>Number</td>
<td>Town</td>
<td>Date</td>
<td>Length</td>
<td>Date</td>
<td>Length</td>
<td>Date</td>
<td>Length</td>
</tr>
<% #max_length.times do |data| %>
<tr>
<td><%= #result.generals.[data].try(:number) %></td>
<td><%= #result.generals.[data].try(:town) %></td>
<td><%= #result.measurements.[data].try(:date) %></td>
<td><%= #result.measurements.[data].try(:length) %></td>
</tr>
<% end %>
</table>
Your code is doing exactly what you are asking it to do: to display a single td for the measurements. In order to have three lots of the columns, you need to loop over the data eg something like:
<table>
<tr>
<th colspan="2">General</th>
<th colspan="6">Measurements</th>
</tr>
<tr>
<td>Number</td>
<td>Town</td>
<td>Date</td>
<td>Length</td>
<td>Date</td>
<td>Length</td>
<td>Date</td>
<td>Length</td>
</tr>
<% #result.measurements.in_groups_of(3).each do |general, measurements| %>
<tr>
<td><%= general.try(:number) %></td>
<td><%= general.try(:town) %></td>
<% measurements.each do |m|
<td><%= m.try(:date) %></td>
<td><%= m.try(:length) %></td>
<% end %>
</tr>
<% end %>
</table>
Note: of course I have not bug-tested this - it will probably not work exactly as is - because I do not have a copy of your data (and you have not provided an example) so you'll need to adapt what I've written to what you have so that it works...
At present I don't know how your general relates to your measurements.
can you provide some example data that you would expect to be returned by the fetching-code and I can adapt this example to fit.

Ruby on Rails - Need Help Optimizing My horrible code involving multiple data models

I am working on a personal project and I have hit a wall. I know I am writing bad code and I really wan to refactor the code below. The application has three tables on the same page. Each table contains data from a has-many-though relationship. In essence I have an employees page which contains three tables of employee licenses that all expire in grouped intervals:
ALL EMPLOYEE PAGE
Employees with licenses Expiring in 30 days
Employees with licenses Expiring in 30-90 days
Employees with licenses Expiring in 90 days
All three of these tables are independently paginated and I am allowing the user to enter a search term and search across all three tables.
However I have over 1200 licenses so the page is taking forever to load. How can I optimize this functionality? Any help would be greatly appreciated.
Model
def self.emp_lic_small
self.all.map{|se| se.employee_licenses.less_than_thirty}.flatten
end
def self.emp_lic_medium
self.all.map{|se| se.employee_licenses.between_thirty_and_ninty}.flatten
end
def self.emp_lic_large
self.all.map{|se| se.employee_licenses.greater_than_ninty}.flatten
end
Controller
#small_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_small.paginate(:page => params[:small_lic], :per_page => 20)
#medium_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_medium.paginate(:page => params[:med_lic], :per_page => 20)
#large_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_large.paginate(:page => params[:large_lic], :per_page => 20)
View
<div class="panel panel-danger">
<div class="panel-heading"><strong>Employee Licenses Expiring in Less Than 30 Days</strong></div>
<table class="table">
<thead>
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">License Name</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
</thead>
<tbody>
<% if #small_employee_licenses.present? %>
<% #small_employee_licenses.each do |e| %>
<tr>
<td class="text-center"><%= link_to e.site_employee.to_s, site_employee_path(e.site_employee)%></td>
<td class="text-center"><%= link_to e.site_employee.site.name, site_path(e.site_employee.site)%></td>
<td class="text-center"><%= e.license.name %></td>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate #small_employee_licenses, param_name:'small_lic' unless #small_employee_licenses.blank? %>
<div class="panel panel-warning">
<div class="panel-heading"><strong>Employee Licenses Expiring in 30-90 Days</strong></div>
<table class="table">
<thead>
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">License Name</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
</thead>
<tbody>
<% if #medium_employee_licenses.present? %>
<% #medium_employee_licenses.each do |e| %>
<tr>
<td class="text-center"><%= link_to e.site_employee.to_s, site_employee_path(e.site_employee)%></td>
<td class="text-center"><%= link_to e.site_employee.site.name, site_path(e.site_employee.site)%></td>
<td class="text-center"><%= e.license.name %></td>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate #medium_employee_licenses, param_name:'med_lic' unless #medium_employee_licenses.blank? %>
<div class="panel panel-success">
<div class="panel-heading"><strong>Employee Licenses Expiring in 30-90 Days</strong></div>
<table class="table">
<thead>
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">License Name</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
</thead>
<tbody>
<% if #large_employee_licenses.present? %>
<% #large_employee_licenses.each do |e| %>
<tr>
<td class="text-center"><%= link_to e.site_employee.to_s, site_employee_path(e.site_employee)%></td>
<td class="text-center"><%= link_to e.site_employee.site.name, site_path(e.site_employee.site)%></td>
<td class="text-center"><%= e.license.name %></td>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate #large_employee_licenses, param_name:'large_lic' unless #large_employee_licenses.blank? %>
What if you have single model and related DB table licenses where you'd have column expiring. In particular column you can do some enum marking licences "30 days", "30-89 days", "90+ days".
I imagine it could look something like this:
class License < ApplicationRecord
#probaly this model has_many / balongs_to something
enum expiring: { 30_days: 1, 30_89_days: 2, 90_days: 3}
end
Then you can query to retreive records according to particular expiring value. If you need to populate values in 3 tables, I guess you can retreive them from single table and then by that expiring column split accordingly. My thinking would be do everything in single table, which then visually you can split in 3.
In addition for better performance you can check Ajax Datatables gem.

Empty list of attributes to change on Ruby on Rails

I am trying to submit a grid form like the below one,
show.html.erb
<%= form_for :datadef, url: update_all_vmodule_path do |fdf|%>
<table id="csvfiles" class="display" width="100%">
<thead>
<tr>
<th width="10%">Column No</th>
<th width="20%">Column name</th>
<th width="20%">Data type</th>
<th width="15%">Column format</th>
<th width="10%">Length</th>
<th width="25%">Comments</th>
</tr>
</thead>
<tbody>
<% #datadefrecords.each do |ddre| %>
<%= fields_for "datadef_records[]", ddre do |dr_fld| %>
<tr>
<td><%= ddre.column_no %></td>
<td><%= ddre.column_name %></td>
<td><%= dr_fld.collection_select :column_datatype, Datadef.select(:column_datatype).uniq, :column_datatype, :column_datatype, {:prompt => true} %> </td>
<td><%= dr_fld.text_field :column_format %></td>
<td><%= dr_fld.text_field :column_length %></td>
<td><%= dr_fld.text_field :comments %></td>
</tr>
<% end %>
<% end %>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td colspan="2"><%= fdf.submit 'Save' %> <%= link_to("<button>Cancel</button>".html_safe, cancelmodal_vmodule_path ) %></td>
</tr>
</tbody>
</table>
<% end %>
<% end %>
This the method i am calling it to submit
def update_all
session[:datadef] = nil
#updatedatadef = Datadef.all.where(:id => params[:datadef_records]).update_all(update_all_params)
redirect_to migproject_path(params[:vmodule][:migproject_id])
end
This is the strong parameters
def update_all_params
params.require(:datadef_records).permit( :column_datatype, :column_format, :column_length, :comments)
end
This is how i get the parameters from form
{"utf8"=>"✓",
"authenticity_token"=>"QrX8JYYYQOlfwKgAABJd0+A7VpugS2Y6n8doDsuKqeM=",
"datadef_records"=>{"12"=>{"column_datatype"=>"varchar",
"column_format"=>"2",
"column_length"=>"2",
"comments"=>"2"},
"13"=>{"column_datatype"=>"varchar",
"column_format"=>"2",
"column_length"=>"2",
"comments"=>"2"}},
"commit"=>"Save",
"id"=>"2"}
But i get this "Empty list of attributes to change" error which is not allowing me to write it in table. And i am not able to identify what could be the error.
Thanks in advance.
I think the problem is to do with your use of update_all, although I'm not 100% sure as to the syntax of the problem.
From what I understand, update_all needs a hash of actual params to populate your data; even then, it will only update what you pass to it (kind of like update_attributes).
Here's a good reference:
data = params[:datadef_records]
#updatedatadef = Datadef.update(data.keys, data.values)
This negates the use of your strong_params, which I'd recommend using. I'd have to spend some time thinking about getting it to work.

Ruby on rails: filtering search box

I am creating a rails application that replicates an online dictionary. I have already implemented the alphabetical_paginate gem but I want to add a search bar that can filter through the words as you type for the word you're looking for. Is there a gem that could do this or is there a straight forward way of filtering with a search bar?
My controller looks like this:
def index
#words, #alphaParams = Word.all.alpha_paginate(params[:letter], {:default_field => "all"}){|word| word.word}
end
View:
<%= alphabetical_paginate #alphaParams %>
<table>
<thead>
<tr>
<th>Word</th>
<th>Wordtype</th>
<th>Description</th>
<th colspan="3"></th>
</tr>
</thead>
<div id="pagination_table">
<tbody>
<% #words.each do |word| %>
<tr>
<td><%= word.word %></td>
<td><%= word.wordtype %></td>
<td><%= word.description %></td>
<td><%= link_to 'Show', word %></td>
</tr>
<% end %>
</tbody>
</div>
</table>
Datatables is what you are looking for.
Here are the links to get started:
http://datatables.net/
https://github.com/rweng/jquery-datatables-rails

Rails 3: retrieving different attribute depending on loop count?

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.

Resources