I'm trying to complete a table iterating in groups of data - ruby-on-rails

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>`

Related

has_many loop undefined method

I have a has_many association in which I want to display in a table. Each Agent can make up to 3 sales, but I want to list them in the same row as the Agent.
undefined method `each' for nil:NilClass on the <% sales.each do |s| %> line.
I'm getting the above error, event though there is defiantly data present for each column
<table>
<tr>
<td>Agent</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
</tr>
<% #agent.sales.each do |agent, sales| %>
<tr>
<td><% agent.name %></td>
<% sales.each do |s| %>
<td><%= s.date %></td>
<td><%= s.product %></td>
<td><%= s.price %></td>
<% end %>
</tr>
<% end %>
</table>
To start, since #agent.sales is an ActiveRecord_Associations_CollectionProxy, you can think of this object more like an array, rather than a hash.
How you initially wrote your loop it appears you were treating it like a hash:
#agent.sales.each do |agent, sale| > this syntax is saying for each key-value pair in the hash, do something with the key (agent) and value (sale).
The second time you try to run a loop, you're running it on the "value" part of the iterator, which does not exist, hence the error on the line sales.each do |sale|
Really, you are very close but are overthinking it. Just drop your first iterator and keep the rest:
<tr>
<td><% #agent.name %></td>
<% #agent.sales.each do |s| %>
<td><%= s.date %></td>
<td><%= s.product %></td>
<td><%= s.price %></td>
<% end %>
</tr>
By the way, you can apply the same principle and refactor your headers too:
<tr>
<td>Name</td>
<% 3.times do |n| %>
<td>Date</td>
<td>Product</td>
<td>Price</td>
<% end %>
</tr>
This assumes that your constraint is 3 max sales.
This should work:
<table>
<tr>
<td>Agent</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
<td>Date</td>
<td>Product</td>
<td>Price</td>
</tr>
<% #agent.sales.each do |sale| %>
<tr>
<td><% #agent.name %></td>
<td><%= sale.date %></td>
<td><%= sale.product %></td>
<td><%= sale.price %></td>
</tr>
<% end %>
</table>

How to create a non-static HTML table header and rows

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.

How to make a nested table in rails

I want to create a table named groups that lists of each group. Inside of each group, I want to list the groups courses. The group has_many :courses, and the course belongs_to :group. My current attempt does not work, and I have no idea where to go from there
Current Attempt
courses_controller.rb
def index
#courses = Course.find_by(group: :group_id)
#groups = Group.all
end
index.html.erb
<table>
<thead>
<tr>
<th>Title</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #groups.each do |group| %>
<tr>
<td><%= group.title %></td>
<td><%= link_to 'Show', group %></td>
<td><%= link_to 'Edit', edit_group_path(group) %></td>
<td><%= link_to 'Destroy', group, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<tr>
<td>
<table>
<thead>
<tr>
<th>Title</th>
</tr>
</thead>
<tbody>
<% #courses.each do |course| %>
<tr>
<% if Lesson.exists?(user: current_user, course: course) %>
<td><%= link_to course.title, edit_lesson_path(Lesson.find_by(user: current_user, course: course)) %></td>
<% else %>
<td><%= link_to course.title, new_course_lesson_path(course) %></td>
<% end %>
<% if current_user.master? %>
<td><%= link_to 'Edit', edit_course_path(course) %></td>
<td><%= link_to 'Destroy', course, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% else %>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<br>
<% if current_user.master? %>
<%= link_to 'New Course', new_course_path %>
<% end %></td>
</tr>
<% end %>
</tbody>
</table>
Any help is appreciated :)
UPDATE
:group_id is the groups id for each created course, since the group has_many :courses The lesson belongs_to :course but that isnt important, as I just want the table, Thanks XP
As I said in my comment, what do you mean it isn't working? Are you getting any error messages? How have you structured you models? Have you created a migration to add group id to courses? What does your routes file look like - are courses nested under groups? If you don't provide this kind of information, its difficult to provide help.
If you look in the logs, you'll get some messages which will direct you to where things are going wrong. Have you tried running
Course.find_by(group: :group_id)
in a rails console. I suspect you'll find that this line is causing problems. Comment out this line and amend your erb as follows (I've not included all the code, but just to give you an idea). Also its not a good idea to use tables within tables.
<table>
<thead>
<tr>
<th>Title</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #groups.each do |group| %>
<tr>
<td><%= group.title %></td>
</tr>
<tr>
<td>
<table>
<thead>
<tr>
<th>Title</th>
</tr>
</thead>
<tbody>
<% #group.courses.each do |course| %>
<tr>
<% if Lesson.exists?(user: current_user, course: course) %>
<td><%= link_to course.title, edit_lesson_path(Lesson.find_by(user: current_user, course: course)) %></td>
<% else %>
<td><%= link_to course.title, new_course_lesson_path(course) %></td>
<% end %>

Deploying rails app on heroku, not displaying SQL query

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

search the contents of a table in rails

I want to place a searchbox on my table to filter it according to eventtitle.
Any ideas how I can go about this? I've tried two tutorials but it didn't work. I've also tried the filterrific gem but it didnt work either.
So far I just have this loading my table in the controller
#newevents = Event.order("id").page(params[:page]).per(50)
And this in my view
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>EventName</th>
<th>Date</th>
<th>Time</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<% #newevents.each do |ne| %>
<% if ne.eventready.blank?%>
<tr>
<td><%= ne.id %></td>
<td><%= ne.eventname %></td>
<td><%= ne.date %></td>
<td><%= ne.time %></td>
<td><%= link_to "Edit Event", edit_event_path(ne.id), class: "btn btn-info" %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<%= paginate #newevents %>

Resources