Rails looping through params data - ruby-on-rails

I am parsing an excel file and passing the data to my controller but I cannot seem to loop through it in my view:
Params:
Parameters: {"data"=>{"consult_charges"=>[{"id"=>"17474", "item"=>"Consultation", "name"=>"Ramon", "price"=>"25.0"}, {"id"=>"17584", "item"=>"Consultation", "name"=>"Ramon", "price"=>"25.0"}, {"id"=>"17490", "item"=>"Consultation", "name"=>"Elizabeth", "price"=>"25.0"}, {"id"=>"17515", "item"=>"Consultation", "name"=>"Elizabeth", "price"=>"25.0"}, {"id"=>"17554", "item"=>"Consultation", "name"=>" Elizabeth", "price"=>"25.0"}, {"id"=>"17623", "item"=>"Consult - Referral Card", "name"=>"Elizabeth", "price"=>"0.0"}, {"id"=>"17486", "item"=>"Consultation", "name"=>"Racha", "price"=>"25.0"}
Controller:
#consult_charges = params["data"]["consult_charges"]
View:
<table class="table awaken">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Item</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<% #consult_charges.each do |sale| %>
<td><%= sale["id"] %></td>
<td><%= sale["name"] %></td>
<td><%= sale["item"] %></td>
<td><%= sale["price"] %></td>
<% end -%>
</tbody>
</table>
The result is a single line of data as if there is only one sale. When I look at the value for #consult_charges it's an array of items like this:
<ActionController::Parameters {"id"=>"17584", "item"=>"Consultation", "name"=>"Ramon", "price"=>"25.0"} permitted: false>
Do I need to do something to convert the type?

The reason it's showing just one line is because <tr> tag is missing on each iteration. Change your view to;
<table class="table awaken">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Item</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<% #consult_charges.each do |sale| %>
<tr>
<td><%= sale["id"] %></td>
<td><%= sale["name"] %></td>
<td><%= sale["item"] %></td>
<td><%= sale["price"] %></td>
</tr>
<% end -%>
</tbody>
</table>

Related

how to find attendance by date range in rails?

manpower has one_to_many association with attendance
invoice table has attribute from_date, to_date
attendance_table has attribute status,manpower_id,and attendance_date.
i am able to render all manpower on invoice#index.
but i dont't know how can i calculate attendance of each manpower and show them in column also considering time duration from invoice table.
in attendance table 1 is present. and 0 is absent.
<table>
<thead>
<tr>
<th>Name</th>
<th>total present</th>
<th>Total Abesent</th>
</tr>
</thead>
<tbody>
<% #manpowers.each do |manpower| %>
<tr>
<td><%= manpower.name %></td>
<td><%= "" %></td>
<td><%= "" %></td>
</tr>
<% end %>
</tbody>
</table>
For Present - manpower.attendance.where(attendance_date: from_date..to_date, status: 1).count
For Absent - manpower.attendance.where(attendance_date: from_date..to_date, status: 0).count
You can create scopes in Attendance model and use those in views.
class Attendance < ActiveRecord::Base
scope :absent, where(status: 0)
scope :present, where(status: 1)
scope :date_between, -> (from_date, to_date) { where(attendance_date: from_date..to_date) }
end
and use it like
For Present - manpower.attendance.date_between(from_date, to_date).present.count
For Absent - manpower.attendance.date_between(from_date, to_date).absent.count
<table>
<thead>
<tr>
<th>Name</th>
<th>Total Present</th>
<th>Total Absent</th>
</tr>
</thead>
<tbody>
<% #manpowers.each do |manpower| %>
<tr>
<td><%= manpower.name %></td>
<td><%= manpower.attendance.date_between(from_date, to_date).present.count %></td>
<td><%= manpower.attendance.date_between(from_date, to_date).absent.count %></td>
</tr>
<% end %>
</tbody>
</table>

Ransack showing all instead of filter

I am trying to use Ransack gem but it returns all 12,000 rows instead of using the filter.
Controller:
def list
#q = LmiHost.ransack(params[:q])
if !(params[:commit].nil?) then
#computers = #q.result
else
#computers = LmiHost.where(:job_id => 121)
end
end
View:
<script type="text/javascript">
$(document).ready(function() {
$("#computer_search_results").dataTable();
});
</script>
<table id="computer_search_table">
<%= search_form_for #q do |f| %>
<tr><td>Project:</td><td><%= f.select :job_id, #project_job_selector %></td></tr>
<tr><td><%= f.submit 'Search' %></td></tr>
<%end%>
</table>
<table id="computer_search_results">
<thead>
<tr>
<th>Lmi Name</th>
<th>Service Tag</th>
<th>Model</th>
<th>Ship Date</th>
<th>Warranty Date</th>
<th>Project</th>
<th>Invoice Name</th>
</tr>
</thead>
<tbody>
<% #computers.each do |computer| %>
<tr>
<td><%= computer.host_description %></td>
<td><%= computer.host_service_tag %></td>
<td><%= computer.host_model %></td>
<td><%= computer.ship_date %></td>
<td><%= computer.warranty_date %></td>
<td><%= computer.job.name unless computer.job.nil? %></td>
<td><%= computer.invoice_name %></td>
</tr>
<% end %>
</tbody>
</table>
the URL looks okay after seraching:
URL
But it is acting like :
#computers = LmiHost.all
Any ideas as too why this is happening?
What you are doing wrong is that you are not specifying a proper matcher. if you want to select particular job id you should indicate the search matcher in the form.
for example you are saying job_id where it should be job_id_eq or what ever matcher you want it to be
where you should getter a Ransack Search item like following.
Ransack::Search<class: LmiHost, base: Grouping <conditions: [Condition <attributes: ["job_id"], predicate: eq, values: ["123"]>], combinator: and>>
in your Ransack search I don't see the predicate hence it returns all the resutls.

Iterate over column names

I have an employee object that I'd like to just show a list of the keys and the associated values for in the show.html.erb file. Here's what I have currently, but how can this be done with a for loop?
<table class="table table-striped">
<tr>
<th>Item</th>
<th>Value</th>
</tr>
<tr>
<td>Name</td>
<td><%= #employee.name %></td>
</tr>
<tr>
<td>Email</td>
<td><%= #employee.email %></td>
</tr>
<tr>
<td>Job Code</td>
<td><%= #employee.job_code %></td>
</tr>
...
</table>
<% #employee.attributes.each do |key, value| %>
<tr>
<td><%= key.gsub('_', ' ').titleize %></td>
<td><%= value %></td>
</tr>
<% end %>

Ruby display array values

I am not a ruby developer. This is the first time I am looking into the code. I want to build a dynamic table for which I have managed below code. However, I am not able to display all the contents of the array except for the first and the last values. How do I display all the values?
Thanks for your help!!
<style>table, td, th{border:1px solid white;}td{padding:5px;}th{background-color:#E0E6EB;color:black;}</style>
<div>
<table border=2>
<tr>
<th width="250px"><B><p style="text-align: center">Name</p></B></th>
<th width="120px"><B><p style="text-align: center">Number</p></B></th>
<th width="60px"><B><p style="text-align: center">Status</p></B></th>
<th width="155px"><B><p style="text-align: center">Product Type</p></B></th>
<th width="60px"><B><p style="text-align: center">Source</p></B></th>
</tr>
<% tempTickets = #subject.PersonAccounts.sorted_by(field("title").in_descending_order) %>
<% cnt = tempTickets.length %>
<% tempTickets.each do |ticket| %>
<div>
<tr>
<td><%= ticket['perfinaccnt-accountname'].first %></td>
<td><%= ticket['perfinaccnt-accountnumber'].first %></td>
<td><%= ticket['perfinaccnt-accountstatus'].first %></td>
<td><%= ticket['perfinaccnt-producttype'].first %></td>
<td><%= ticket['perfinaccnt-accountsrcsystem'].first %></td>
</tr>
</div>
<div>
<tr>
<td><%= ticket['perfinaccnt-accountname'].last %></td>
<td><%= ticket['perfinaccnt-accountnumber'].last %></td>
<td><%= ticket['perfinaccnt-accountstatus'].last %></td>
<td><%= ticket['perfinaccnt-producttype'].last %></td>
<td><%= ticket['perfinaccnt-accountsrcsystem'].last %></td>
</tr>
</div>
<% end %>
</table>
</div>
You could do it with three nested loops:
<%= tempTickets.each do |ticket|
[
'perfinaccnt-accountname',
'perfinaccnt-accountnumber',
'perfinaccnt-accountstatus',
'perfinaccnt-producttype',
'perfinaccnt-accountsrcsystem'
].each do |f|
ticket[f].each do |tf|
puts "<td>tf</td>";
end if ticket[f]
end
end %>
I'm not sure about the data structure here but you could check it by doing an #inspect to tempTickets and all the subsequent objects. From the looks of it "ticket['perfinaccnt-accountname']" is actually an array
and as such you can just loop through it. Not the fastest way to do it but it should work to just do a nested loop.
<style>table, td, th{border:1px solid white;}td{padding:5px;}th{background-color:#E0E6EB;color:black;}</style>
<div>
<table border=2>
<tr>
<th width="250px"><B><p style="text-align: center">Name</p></B></th>
<th width="120px"><B><p style="text-align: center">Number</p></B></th>
<th width="60px"><B><p style="text-align: center">Status</p></B></th>
<th width="155px"><B><p style="text-align: center">Product Type</p></B></th>
<th width="60px"><B><p style="text-align: center">Source</p></B></th>
</tr>
<% tempTickets = #subject.PersonAccounts.sorted_by(field("title").in_descending_order) %>
<% cnt = tempTickets.length %>
<% tempTickets.each do |ticket| %>
<div><tr>
<% ticket.each do |k,v|
v.each do |col| %>
<td><%= col %></td>
<% end %>
<% end %>
</tr></div>
<% end %>
</table>
</div>

Rails create new records based on checked records

I have a table showing time entries (events) related to a workorder.
The user can enter a check on any of the table rows.
For each checked row, I want to create a new record in the invtime table.
invtime belongs_to :event
event has_many :invtimes
This is the table:
<table>
<thead>
<tr>
<th>Title</th>
<th>Employee</th>
<th>Date</th>
<th>Hours</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% Event.where("workorder_id = ?", Invoice.find(#invoice).workorder_id).where("billed = ?", false).each do |event| %>
<tr>
<td><%= check_box_tag(:add_record) %></td>
<td><%= event.title %></td>
<td><%= event.employee.employee_full_name %></td>
<td><%= event.starts_at.strftime("%m/%d/%Y") %></td>
<td><%= event.hours %></td>
<td></td>
</tr>
<% end %>
</tbody>
</table>
I'm not sure how to process the returned page with the checkboxes checked.
Should I use Javascript (coffeescript)? Or can I do it with Ruby?
This should do it:
<tbody>
<%= form_tag(your_path_helper) do %>
<% Event.where("workorder_id = ?", Invoice.find(#invoice).workorder_id).where("billed = ?", false).each do |event| %>
<tr>
<td><%= check_box_tag 'event_ids_to_save[]', value: event.id, checked: Invtime.exists?(event_id: event.id) %></td>
<td><%= event.title %></td>
<td><%= event.employee.employee_full_name %></td>
<td><%= event.starts_at.strftime("%m/%d/%Y") %></td>
<td><%= event.hours %></td>
<td></td>
</tr>
<% end %>
<tr><td colspan='6'><%= submit_tag %></td></tr>
<% end %>
</tbody>
The corresponding action of the controller responding to the form_tag submit:
def action_of_the_form_tag
params[:event_ids_to_save].each do |event_id|
event = Event.where(id: event_id)
# do your logic here
end
end

Resources