I have two arrays, and I'd like to create a table with dynamic header cells (from the first array called subjects) and iteratively add contents (from the second array called examscores) in table rows with respect to the table header value.
Desired outcome is (fiddle):
The erb code is:
<table width="100%" border="1">
<thead>
<tr>
<th rowspan="2" scope="col">NAME</th>
<th colspan="<%= #subjects_by_class.size %>" scope="col">Subjects/Scores</th>
<th rowspan="2" scope="col">Total</th>
<th rowspan="2" scope="col">Average</th>
</tr>
<tr>
<% #subjects_by_class.each do |s| %>
<th> <%= s.name %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #examscore.each do |ex| %>
<tr>
<td><%= get_student_name_by_id(ex.student_id) %></td>
<% #subjects_by_class.each do |ss| %>
<% #examscore.each do |ii| %>
<% if ss.id == ex.subject_id %>
<td> <%= i.total %> </td>
<% break %>
<% end %>
<% end %>
<% end %>
<td><%= sum_student_totalscore(ex.student_id, year_id) %> </td>
<td><%= avg_student_totalscore(ex.student_id, year_id) %></td>
</tr>
<% end %>
</tbody>
</table>
The output I get is (fiddle):
A new tr is created under Maths subject instead of a new td for Arts subject, and this results in Average td being distorted.
Any insight will be greatly appreciated.
Well, just look at this section of your code:
<% #examscore.each do |ex| %>
<tr>
You create a new line for each #examscore, and you have 4 of those (1 per user/subject, so you end up with 4 lines of course).
Your tbody should be something like this:
<tbody>
<% #students.each do |student| %>
<tr>
<td><%= student.name %></td>
<% #subjects_by_class.each do |subject| %>
<% #examscore.each do |score| %>
<% if score.subject_id == subject.id && score.student_id == student.id %>
<td><%= score.total %></td>
<% break %>
<% end %>
<% end %>
<% end %>
<td><%= sum_student_totalscore(student.id, year_id) %> </td>
<td><%= avg_student_totalscore(student.id, year_id) %></td>
</tr>
<% end %>
</tbody>
It's a bit strange that you only care about the year in your totals
Also you could improve things a little bit by having a method in your Student class that returns an array of scores for a given year/list of subjects
# Returns an array of scores for the given year
def scores(year, subject_ids)
subject_ids.map do |subject_id|
# find score for year & the given subject_id
end
end
This way your body would become
<tbody>
<% #students.each do |student| %>
<tr>
<td><%= student.name %></td>
<% #scores = student.scores(year_id, #subjects_by_class) %>
<% #scores.each do |score| %>
<td><%= score.total %></td>
<% end %>
<% scores_total = #scores.sum(&:total) %>
<td><%= scores_total %> </td>
<td><%= scores_total / #scores.size.to_f %></td>
</tr>
<% end %>
</tbody>
which I found more clear, but it could be further improved with decorators for instance.
Related
I know this code is wrong, but I want to know which is the correct form to do it, I'm trying to iterate the groups, and each group have 8 teams
<table class="table">
<thead>
<tr>
<% #groups.each do |group| %>
<th><%= group.name %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #groups.each_slice(4) do |group_a, group_b, group_c, group_d| %>
<% group_a.teams.each do |team_a|, group_b.teams.each do |team_b|, group_c.teams.each do |team_c|, group_d.teams.each do |team_d| %>
<tr>
<td><%= team_a.name %></td>
<td><%= team_b.name %></td>
<td><%= team_c.name %></td>
<td><%= team_d.name %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
Solution actually depends on the table structure you want to see, which you haven't clarified in the question. Here is an attempt; see how this ends up:
<table class="table">
<tbody>
<thead>
<th>Team#1</th>
<th>Team#2</th>
<th>Team#3</th>
<th>Team#4</th>
</thead>
<% #groups.each do |group| %>
<tr>
<th colspan='4'><%= group.name %></th>
</tr>
<tr>
<% group.teams.each do |team| %>
<td><%= team.name %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
Thanks #JagdeepSingh for your help, after try and try this is what im looking for bro:
`<table class="table">
<thead class="thead-dark">
<tr>
<% #groups.each do |group| %>
<th><%= group.name %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #groups.each_slice(4) do |group_a, group_b, group_c, group_d| %>
<% for x in 0..7 do %>
<tr>
<td><%= group_a.teams[x].name %></td>
<td><%= group_b.teams[x].name %></td>
<td><%= group_c.teams[x].name %></td>
<td><%= group_d.teams[x].name %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>`
I'm having issues with rowspan in a loop in my form using conditional statements. My first Rowspan 2 is outside the loop and works fine, but my second Rowspan 2 is inside the conditional, therefore it does not work as it should. Is there a method to resolve this?
This is what I'm trying to achieve
Form
<table>
<th>Header 1</th>
<th>Header 2</th>
<th>Description</th>
<th>Phase</th>
<tr>
<td rowspan="4">Rowspan 4</td>
<td rowspan="2">Rowspan 2</td>
<% Identity.all.each do |identity| %>
<%= form.fields_for :indicators, form.object.indicators.where(identity: identity).first_or_initialize do |ff| %>
<%= ff.hidden_field :id %>
<%= ff.hidden_field :identity_id %>
<% if ff.object.identity.number <= 1.4 %>
<td><%= ff.object.identity.description %></td>
<td><%= ff.collection_select :phase_id, Phase.all, :id, :name %></td>
</tr>
<tr>
<% elsif ff.object.identity.number > 1.4 %>
<td rowspan="2">Rowspan 2</td>
<td><%= ff.object.identity.description %></td>
<td><%= ff.collection_select :phase_id, Phase.all, :id, :name %></td>
<% end %>
</tr>
<% end %>
<% end %>
</table>
This is definitely doable, but I think it's not working for you based on how you're partitioning the data.
Specifically, it seems like you're closing trs in weird places, as well as you create one rowspan outside the loop.
A more robust solution would likely partition the data into rowspan=X blocks (maybe by using group_by, in_groups_of, chunk)
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Description</th>
<th>Phase</th>
</tr>
<tr>
<td rowspan="4">Rowspan 4</td>
<% Identity.all.each_with_index do |identity, index| %>
<% if index.positive? %>
<tr> #emit a tr in every row but the first
<% end %>
<% if index.even? %>
<td rowspan="2">Rowspan 2</td>
<% end %>
<%= form.fields_for :indicators, form.object.indicators.where(identity: identity).first_or_initialize do |ff| %>
<%= ff.hidden_field :id %>
<%= ff.hidden_field :identity_id %>
<td><%= ff.object.identity.description %></td>
<td><%= ff.collection_select :phase_id, Phase.all, :id, :name %></td>
</tr> #Close a tr in every row
<% end %>
<% end %>
</table>
I haven't tested this personally, but I think something closer to this format would work. It would also be useful to see the html output you're actually getting to debug.
can you help me with this problem?
I cannot execute "ActiveRecord::Base.connection.execute("SQL")" on production version (deployed on Heroku), although it works locally.
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Games</th>
<th>Wins</th>
<th>Win Ratio</th>
</tr>
</thead>
<tbody>
<% ActiveRecord::Base.connection.execute("
select * from players").each_with_index do |performer, i| %>
<tr>
<td><%= (i+1) %> </td>
<td><%= performer[0] %> </td>
<td><%= performer[1] %></td>
<td><%= performer[2] %></td>
<th><%= performer[3] %> <%='%'%></th>
</tr>
<% end %>
</tbody>
DEVELOPMENT
PRODUCTION
HEROKU LOGS
This should be a comment (sorry no rep yet)
Since you are calling all players why not do something like
<% #players.each do |p| %>
<td><%= (i+1) %> </td>
<td><%= p.name %> </td>
<td><%= p.games %></td>
<td><%= p.wins %></td>
<% percent = p.wins / p.games %>
<% percent = percent * 100 %>
<% percent = number_with_precision(percent, :precision => 0) %>
<th><%= percent %> %</th>
<% end %>
In controller do something like #players = Player.all
This will remove the need for any SQL statement in your view, removing the problem of having pg and sql
Hi I am using Ruby on Rails to show an table, this is the code in view:
<h1> Show by category</h1>
<h2> table </h2>
<table border="1" style="width:70%">
<tr>
<th>Name</th>
<th>SKU</th>
<th>Category</th>
<th><%= link_to "New",new_record_path %>
</tr>
<%= #records.each do |r| %>
<tr>
<td><%= r.name %></td>
<td><%= r.sku %></td>
<td><%= r.category %></td>
<td><%= link_to "Edit", edit_record_path(r) %></td>
</tr>
<% end %>
<%= will_paginate #records %>
</table>
and I got this
http://i.stack.imgur.com/rUN0v.png
Between the headline and the table, the records are all shown up, I don't know where it is coming from, can somebody help?
The problem is here in this line
<%= #records.each do |r| %>
which should be
<% #records.each do |r| %>
Imp note:
<% %> #Executes the statement/expression.
<%= %> #Prints the output.
wondering whats the best way to split up a loop on a model in the view onto TWO tables. seems simple enough.
<div>
<table>
<tr><th>Refreshments and Exhibits</th></tr>
<% #exhibitor.each do |exhibitor| %>
<tr>
<td><%= exhibitor.name %></td>
</tr>
<% end %>
</table>
</div>
<div>
<table>
<tr><th>Refreshments and Exhibits</th></tr>
<% #exhibitor.each do |exhibitor| %>
<tr>
<td><%= exhibitor.name %></td>
</tr>
<% end %>
</table>
</div>
this displays the same table twice. i want to loop through the #exhibitor to fill the td's in the first table, limit 15. then continue looping through the rest of the exhibitor.name for the second table.
If you want tables of 15 do this
<% #exhibitors.each_slice(15) do |exhibitors_group| %>
<div>
<table>
<tr><th>Refreshments and Exhibits</th></tr>
<% exhibitors_group.each do |exhibitor| %>
<tr>
<td><%= exhibitor.name %></td>
</tr>
<% end %>
</table>
</div>
<% end %>
If you want first 15 and the rest in other table do this
<div>
<table>
<tr><th>Refreshments and Exhibits</th></tr>
<% #exhibitor[0..15].each do |exhibitor| %>
<tr>
<td><%= exhibitor.name %></td>
</tr>
<% end %>
</table>
</div>
<div>
<table>
<tr><th>Refreshments and Exhibits</th></tr>
<% #exhibitors[16..-1].each do |exhibitor| %>
<tr>
<td><%= exhibitor.name %></td>
</tr>
<% end %>
</table>
</div>
You should also consider 2 things:
Use a helper or a layout for those tables
Instead of slicing the array in the view do it in the controller