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.
Related
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 :-)
I'm creating a rails app for my expenses. I have several models like user, spending, currency. I put relations between them, everything works. I can add a new expense that is shown on a index (all the expenses from all users) and I decided to loop through the expenses on the user show page, where the current_user see his own expenses.
I also grouped the expenses by date (month and year) and I have a "Today" group, which shows all the expenses entered on the day. It looks like this in the controller :
def show
#user_spendings = #current_user.spendings.all.order('date DESC').paginate(:page => params[:page], :per_page =>
10)
#Retrives all messages and divides into two groups todays messages and other messages
#grouped_spendings = #user_spendings.group_by{ |t| t.date.to_date == DateTime.now.to_date }
if #user_spendings.present?
#Create month wise groups of messages
#month_wise_sorted_spendings = #user_spendings.group_by{ |t| (Date::MONTHNAMES[t.date.month] + " " + t.date.year.to_s) }
end
end
This is my view :
<% if #grouped_spendings.present? && #grouped_spendings[true].present? %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Amount</th>
<th>Currency</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<h3>Today</h3>
<tbody>
<% #grouped_spendings[true].each do |spending| %>
<tr>
<td><%= spending.title %></td>
<td><%= spending.description %></td>
<td><%= '%.02f' % spending.amount %></td>
<td><%= spending.currency.symb %></td>
<td><%= spending.date.strftime('%d %B %Y') %></td>
</tr>
</tbody>
<% end %>
</table>
<% end %>
<% if #month_wise_sorted_spendings.present? %>
<% #month_wise_sorted_spendings.each do |hash_elements|%>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Amount</th>
<th>Currency</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<% date_in_link = "#{hash_elements.first}" %>
<h3><%= link_to(date_in_link, detail_result_path) %></h3>
<tbody>
<% hash_elements.last.each do |spending| %>
<tr>
<td><%= spending.title %></td>
<td><%= spending.description %></td>
<td><%= '%.02f' % spending.amount %></td>
<td><%= spending.currency.symb %></td>
<td><%= spending.date.strftime('%d %B %Y') %></td>
</tr>
</tbody>
<% end %>
</table>
<% end %>
<% end %>
I would like to create a link on the month and year and open a page where all the informations are looped again but only for this specific month of the year.
I already achieved the linking with this :
<% date_in_link = "#{hash_elements.first}" %>
<h3><%= link_to(date_in_link, detail_result_path) %></h3>
To avoid mixing the controllers etc... I decided to create new controller called detail and a result.html.erb page where I will loop the expenses about the specific month and year.
Which way should I take with this ? Is it a good option to do a new controller ? And how can I get the infos back according to the date on my new page ?
I thought about passing some params via the link_to but I'm not sure it's the best solution. Maybe there is something easier but I don't know or see it.
If anyone can help would be great !
Thanks alot
I found the answer but it's on a other post I did as I had an other problem before this one. So the link is here
Feel free to ask question if any
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.
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
Iam having an issue getting data from two different database tables to display properly with in an HTML table. I'm sure I am just overlooking something and this is pretty easy, but for whatever reason I cannot get quantity to display, or the <td class="sub-total"> to display either.
Demands (you can think of these as orders) and items are connected through a table called demand_items. The demand_items table also has the quantity of the item ordered. The page will display but the quantity and the subtotal will not render.
Here is the html:
<h3>Order for <%= #demand.customer.name %></h3>
<h4>Ordered for: <%= #demand.date %></h4>
<h4>Add Items</h4>
<%= render 'demands/item_form' %>
<table class="customer-table">
<tr>
<th>Item</th>
<th>Price</th>
<th>Quantity</th>
<th>Sub Total</th>
</tr>
<% #demand.items.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.price %></td>
<% #demand.demand_items do |quantity| %>
<td><%= quantity.quantity %></td>
<td class="sub-total"><%= (item.price) * (quantity.quantity) %></td>
<% end %>
<% end %>
</tr>
</table>
#demand.demand_items.each do |quantity|..........instead of #demand.demand_items do |quantity|......