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>
Related
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.
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>
I am trying to render all the data of a stored procedure in a table in my view but the problem is that the name of the columns can change in each stored query so I can not make the specific field call in the render because each query brings different columns, how could I make the call of the columns name for the (table titles) and render all the data in the unspecified in the name of the data but bring all the results of the query that apparently returns them in an array?
this is my stored_procedures_controller.rb
class StoredProceduresController < ApplicationController
before_action :authenticate_usuario!
def cantidad_productosxpedido
#cantidad_productosxpedido = Stored_procedure.fetch_db_records("exec SPAD_ReporteProductosxPedido '#{Time.now.try(:strftime,"%Y%m%d")}','#{params[:search]}','#{params[:search6]}'")
respond_to do |format|
format.html
format.js
format.xls
end
end
end
this is my model stored_procedure.rb
class Stored_procedure < ActiveRecord::Base
def self.fetch_db_records(proc_name_with_parameters)
connection.select_all(proc_name_with_parameters)
end
def self.insert_update_delete_calculate(proc_name_with_parameters)
connection.execute(proc_name_with_parameters)
end
def self.fetch_val_from_sp(proc_name_with_parameters)
connection.select_value(proc_name_with_parameters)
end
end
I usually do the rendering of the stored this way
<table class="table table-striped table-bordered table-condensed table-hover rwd_auto">
<thead>
<tr>
<th>Ruta</th>
<th>Pedido</th>
<th>IdCliente</th>
<th>Cliente</th>
<th>Nombre Comercial</th>
<th>2010100</th>
<th>2020100</th>
<th>2020200</th>
<th>2020500</th>
<th>2021200</th>
<th>2200500</th>
<th>2200501</th>
<th>2200508</th>
<th>2200509</th>
<th>2201000</th>
<th>2203100</th>
<th>2203101</th>
<th>2203102</th>
<th>2203104</th>
<th>2203108</th>
<th>2203200</th>
</tr>
</thead>
<tbody id="container_productosxpedido">
<%= render partial: "/stored_procedures/cantidad_productoxpedido", collection: #cantidad_productosxpedido %>
</tbody>
</table>
and in the partial _cantidad_productoxpedido
<tr>
<td><%= cantidad_productoxpedido['Ruta'] %></td>
<td><%= cantidad_productoxpedido['Pedido'] %></td>
<td><%= cantidad_productoxpedido['IdCliente'] %></td>
<td><%= cantidad_productoxpedido['Cliente'] %></td>
<td><%= cantidad_productoxpedido['NombreCorto'] %></td>
<td><%= cantidad_productoxpedido['2010100'] %></td>
<td><%= cantidad_productoxpedido['2020100'] %></td>
<td><%= cantidad_productoxpedido['2020200'] %></td>
<td><%= cantidad_productoxpedido['2020500'] %></td>
<td><%= cantidad_productoxpedido['2021200'] %></td>
<td><%= cantidad_productoxpedido['2200500'] %></td>
<td><%= cantidad_productoxpedido['2200501'] %></td>
<td><%= cantidad_productoxpedido['2200508'] %></td>
<td><%= cantidad_productoxpedido['2200509'] %></td>
<td><%= cantidad_productoxpedido['2201000'] %></td>
<td><%= cantidad_productoxpedido['2203100'] %></td>
<td><%= cantidad_productoxpedido['2203101'] %></td>
<td><%= cantidad_productoxpedido['2203102'] %></td>
<td><%= cantidad_productoxpedido['2203104'] %></td>
<td><%= cantidad_productoxpedido['2203108'] %></td>
<td><%= cantidad_productoxpedido['2203200'] %></td>
</tr>
but when changing the name of the columns I do not know how to do to display the data without specifying the names of those columns
UPDATE
I could resolve to display the contents of the table as follows
<%#cantidad_productosxpedido.to_a.each do |foo|%>
<tr>
<%foo.each do |label, value|%>
<td><%= value %></td>
<%end%>
</tr>
<%end%>
but now I try to add the headers of the table
like this:
<%#cantidad_productosxpedido.first.each do |foo|%>
<tr>
<%foo.each do |label, value|%>
<th><%= label %></th>
<%end%>
</tr>
<%end%>
but it does not show correct in the table but renders it as follows
<tr>
<th>title1</th> <td>value1</th>
</tr>
<tr>
<th>title2</th> <th>value2</th>
</tr>
object.attributes.keys will return an array of column names for a given record. You can use that to generate your columns as follows:
<% #cantidad_productosxpedido.each do |cantidad_productoxpedido| %>
<% cantidad_productoxpedido.keys.each do |col| %>
<td><%= cantidad_productoxpedido[col] %></td>
<% end %>
<% end %>
Not tested, but that should work.
This is my first time to use ruby on rails. I just want to know on how to get the product if I multiply the product_cost and quantity_sold even every time I add/input another sold product/Item. And also how to count its over all total sales. This is where I want to put the code. I would really appreciate your answers and suggestions. Thank you!
Inside the index.
views\solds\index
<p I'd="notice"><℅= notice ℅><p>
<h1><List of Sold Product></h1>
<table>
<thead>
<tr>
<th>Product Category</th>
<th>Product name</th>
<th>Product code</th>
<th>Product desc</th>
<th>Product cost</th>
<th>Quantity sold</th>
<th>Date sold</th>
<th colspan="3"></th>
</tr>
<thead>
<tbody>
<℅ #solds.each do |sold| %>
<tr>
<td><%= sold.product_category %></td>
<td><%= sold.product_name %></td>
<td><%= sold.product_code %></td>
<td><%= sold.product_desc %></td>
<td><%= sold.product_cost %</td>
<td><%= sold.quantity_sold %></td>
<td><%= sold.date_sold %></td>
<td><%= link_to 'Show', sold %></td>
<td><%= link_to 'Edit',edit_sold_path(sold) %></td>
<td><%= link_to 'Destroy', sold, method: :delete, data: { confirm: 'Are you sure?' } %></td>.
</tr>
<℅ end ℅>
</tbody>
</table>
<br>
<%= link_to 'New Sold', new_sold_path %>
You can also go with:
Total Cost : <%= #solds.sum(:product_cost) %>
Total Quantity : <%= #solds.sum(:quantity_sold) %>
Add This code below of the table listing
Just Add this below table
Total Cost : <%= #solds.reduce(0){|s, sum| sum + s.product_cost} %>
Total Quantity : <%= #solds.reduce(0){|s, sum| sum + s.quantity_sold} %>
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 %>