Creating large bootstrap table in Ruby on Rails - ruby-on-rails

Hi I am a little new to Ruby on Rails and this should be pretty simple. I want to make a large bootstrap table, while also being able to loop through my model/database table.
<table class="table">
<thead>
<tr>
<th scope="col">I</th>
<th scope="col">Id</th>
<th scope="col">Time</th>
<th scope="col">Duration</th>
<th scope="col">Price</th>
<th scope="col">Name</th>
<th scope="col">Company</th>
<th scope="col">City</th>
</tr>
</thead>
<tbody>
<% 96.times |i| %>
<% #workorders.each do |workorder| %>
<tr>
<td><%= i %></td>
<td><%= workorder.id %></td>
<td><%= workorder.time %></td>
<td><%= workorder.duration %></td>
<td><%= workorder.price %></td>
<td><%= workorder.name %></td>
<td><%= workorder.company %></td>
<td><%= workorder.city %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
</div>
I have verified that when I do just a .each loop over #workorders it works fine. However, when I run my Rails application with the above code I get a syntax error, nothing too detailed. What would be the proper syntax to create this 96-length bootstrap table, while also looping through my model/sqlite table?

Brilliant! Thank you so much! Adding a do after 96.times solved everything.

Related

Ruby on Rails using pluck to select singular dates from table?

Hi I am trying to build a rails meetings application. I have aa table called Meetings with 4 columns name:string start_date:datime end_date:datetime duration:integer
A meeting will have multible inputs per day for example a person can have two or three different meetings lasting different times throughout the day see image below
I want to be able to show one date per mutiple meetings and it tried using pluck but i get output = ["2021-08-01", "2021-08-02", "2021-08-06", "2021-08-07", "2021-08-10", "2021-08-05", "2021-08-28", "2021-08-29"]
this code works outside the table but not the way i would hope
<%= Meeting.distinct.pluck('date(start_date)') %>
How do i get it working within my meeting.index.erb table view ?
<h1>Meetings</h1>
<table class="table">
<thead class="thead-dark">
<tr>
<th>Name</th>
<th>Start date</th>
<th>End date</th>
<th>Duration</th>
<th>Duration Time</th>
</tr>
</thead>
<tbody>
<% #meetings.each do |meeting| %>
<tr>
<td><%= meeting.name %></td>
<td><%= meeting.start_date.strftime("%d-%b-%Y %I:%M %p") %></td>
<td><%= meeting.end_date.strftime("%d-%b-%Y %I:%M %p") %></td>
<td><%= meeting.duration %></td>
<td><%= duration_display meeting %></td>
</tr>
<% end %>
</tbody>
</table>
<br><br>
Maybe i am going about it the wrong if someone could point me in the best direction i would appreciate it
There are probably many ways to do this. One idea is to pluck out the individual dates by day,
then pull in the data that matches that date range using a range condition.
https://guides.rubyonrails.org/active_record_querying.html#range-conditions
<h1>Meetings</h1>
<table class="table">
<thead class="thead-dark">
<tr>
<th>Name</th>
<th>Start date</th>
<th>End date</th>
<th>Duration</th>
<th>Duration Time</th>
</tr>
</thead>
<tbody>
<% #meetings.pluck("start_date").map{ |x| x.to_date}.sort.uniq.each do | date | %>
<tr>
<td> <%= date.strftime("%d-%b-%Y") %> </td>
</tr>
<% #meetings.where(start_date: (date.to_time)..date.to_time+86399).each do | meeting | %>
<tr>
<td><%= meeting.name %></td>
<td><%= meeting.start_date.strftime("%d-%b-%Y %I:%M %p") %></td>
<td><%= meeting.end_date.strftime("%d-%b-%Y %I:%M %p") %></td>
<td><%= meeting.duration %></td>
<td><%= duration_display meeting.duration %></td>
</tr>
<% end %>
</tbody>
<% end %>
</table>
<br><br>

Nested loop in table ruby on rails 6 view wrong order

I have a category, which has work registrations. To show these in a table i use code below. However it gets executed in the wrong way. All categories are printed in the top of the table, while all work registrations are then posted below the list of categories. This is strange to me, as the work registrations loop is executed in the category loop. Any idea how to correct the code? I tried using group_by, however this gives the same result.
<table id="dataWorkRegistrations" class="table table-lightborder" data-class="WorkRegistration">
<thead>
<tr>
<th><%= t('work_registration.fields.code.label') %></th>
<th><%= t('work_registration.fields.status.label') %></th>
<th><%= t('work_registration.fields.name.label') %></th>
<th><%= t('work_registration.fields.frequency.label') %></th>
<th><%= t('work_registration.fields.amount.label') %></th>
</tr>
</thead>
<tbody>
<% #categories.each do |categorie| %>
<tr class='bg-light'>
<td><b><%= categorie.code %></b></td>
<td> </td>
<td><b><%= categorie.name %></b></td>
<td> </td>
<td> </td>
</tr>
<% categorie.work_registrations.each do |work_registration| %>
<tr>
<td><%= work_registration.code %></td>
<td><%= work_registration.status %></td>
<td><%= work_registration.name %></td>
<td><%= work_registration.frequency %></td>
<td><%= work_registration.amount %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
I managed to solve the problem. As the table as an index for work_registrations. Datatables started to sort all, which caused the strange sorting. After disabling initial sorting on datatable, it worked instant.

Sort Date (Newest to Oldest) ROR

this is index.html.erb
<%= link_to_modal_new(new_master_film_path, "Enter film") %>
<%= link_to_export("Master films", params) %>
<%= paginate #master_films %>
<table class="table table-condensed table-hover">
<thead>
<tr>
<th></th>
<th>Serial</th>
<th>Formula</th>
<th>Mix/g</th>
<th>Mach</th>
<th>ITO top</th>
<th>Thinky</th>
<th>Chemist</th>
<th>Operator</th>
<th>Inspector</th>
<th>Eff W</th>
<th>Eff L</th>
<th>Yield</th>
<th>Defects</th>
<th>Laminated</th>
<th>Note</th>
</tr>
</thead>
<tbody>
<%= render #master_films %>
</tbody>
</table>
I need to organize the Date column in desc (newest to oldest) dates. I added the following dropdown option but it fails. Error template. Or if I can default the sort to newest Laminated date is first.
<%= render 'shared/sort_dropdown', current: sort, choices: [['serial','desc'], ['laminated','desc']] %>
I am writing this in detail, assuming you are very new to rails because of the way you asked the question and the way your code of view is. My apologies, if I am wrong.
In your controller (I guess it should be films_controller.rb or master_films_controller.rb), the action in which you are calculating this #master_films (it will have same name as the view that has the code you posted in question), add this:
#master_films = MasterFilm.all.order('laminated DESC')
# OR
#master_films = MasterFilm.all.order(laminated: 'desc')
# letter case does not matter
Your table code in view is not correct. It should be:
<table class="table table-condensed table-hover">
<thead>
<tr>
<th>Serial</th>
<th>Formula</th>
<th>Mix/g</th>
<th>Mach</th>
<th>ITO top</th>
<th>Thinky</th>
<th>Chemist</th>
<th>Operator</th>
<th>Inspector</th>
<th>Eff W</th>
<th>Eff L</th>
<th>Yield</th>
<th>Defects</th>
<th>Laminated</th>
<th>Note</th>
</tr>
</thead>
<tbody>
<%= #master_films.each do |master_film| %>
<tr>
<td><%= master_film.serial %></td>
<td><%= master_film.formula %></td>
<td><%= master_film.mix %></td>
<td><%= master_film.mach %></td>
<td><%= master_film.ito_top %></td>
<td><%= master_film.thinky %></td>
<td><%= master_film.chemist %></td>
<td><%= master_film.operator %></td>
<td><%= master_film.inspector %></td>
<td><%= master_film.eff_w %></td>
<td><%= master_film.eff_l %></td>
<td><%= master_film.yield %></td>
<td><%= master_film.defects %></td>
<td><%= master_film.laminated %></td>
<td><%= master_film.note %></td>
<tr>
</tbody>
</table>

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

App deployment to heroku does not show dynamic contents on the page

Hi i am new to web development, learning and working on rails 4.2.4 version. I have created a simple blog app that is working absolutely fine on local server, but when i tried to push it on HEROKU only my static are visible on page but not the dynamic contents . where might i be making mistake.please help below is my code for index view.
<table>
<thead>
<tr>
<th>Title</th>
<th>Body</th>
<th>Category</th>
<th>Author</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #posts.each do |post| %>
<tr>
<td><%= link_to post.title, post %></td>
<td><%= post.body %></td>
<td><%= post.category_id %></td>
<td><%= post.author_id %></td>
</tr>
<% end %>
</tbody>
</table>
<br>

Resources