Iterate in array of hashes in side array in ruby - ruby-on-rails

I want to iterate this oject and print in table
[
{
:invoice_details=>
{
:customer_name=>"Dylan Sollfrank",
:invoice_number=>"1060",
:invoice_status=>"paid",
:transaction_total_amount=>50.0,
:trnsaction_details=>
[
{
:transaction_number=>"QB1601361635",
:customer_name=>"Dylan Sollfrank",
:amount=> {:amount_to_pay=>50.0, :payment_fee=>0.0},
:created_time=>"12:10 PM",
:created_date=>"Sep 29,2020",
:payment_method=>"Quickbook",
:payment_status=>"completed"
}
]
}
}
]
invoice_details in one row and transaction_details in another row in table format. transaction_details is an array inside the invoice_details.
EDIT
I did it by
I did it
<% ar_activity.first.each do |invoice, invoices_hash| %>
<tr>
<td><%= invoices_hash[:invoice_number] %> </td>
<td><%= invoices_hash[:invoice_status]%></td>
<td><%= invoices_hash[:customer_name] %> </td>
<td><%= invoices_hash[:transaction_total_amount]%></td>
</tr>
<% invoices_hash[:trnsaction_details].each do |transaction|%>
<tr>
<td></td>
<td><%= transaction[:transaction_number]%></td>
<td><%= transaction[:customer_name]%></td>
<td><%= transaction[:amount][:amount_to_pay].to_f + transaction[:amount][:payment_fee].to_f%></td>
<td><%= "#{transaction[:created_time]} " "#{transaction[:created_date]}" %></td>
<td><%= transaction[:payment_method] %></td>
<td><%= transaction[:payment_status] %></td>
</tr>
<%end%>
<%end%>
but its only good for the first invoice_details so it's not working
Thanks

I did it
<% ar_activity.each do |invoices|%>
<% invoices.each do |invoice, invoices_hash| %>
<tr>
<td><%= invoices_hash[:invoice_number] %> </td>
<td><%= invoices_hash[:invoice_status]%></td>
<td><%= invoices_hash[:customer_name] %> </td>
<td><%= invoices_hash[:transaction_total_amount]%></td>
</tr>
<% invoices_hash[:trnsaction_details].each do |transaction|%>
<tr>
<td></td>
<td><%= transaction[:transaction_number]%></td>
<td><%= transaction[:customer_name]%></td>
<td><%= transaction[:amount][:amount_to_pay].to_f + transaction[:amount][:payment_fee].to_f%></td>
<td><%= "#{transaction[:created_time]} " "#{transaction[:created_date]}" %></td>
<td><%= transaction[:payment_method] %></td>
<td><%= transaction[:payment_status] %></td>
</tr>
<%end%>
<%end%>
<%end%>

Related

Rails when I wrap a table with a form the table disappears checkbox

I am not sure what is happening here. When I wrap the form around the table the table disappears. When I remove the form, the table reappears. Thanks in advance. Here is the code:
<% form_tag deposit_checks_path :method => :put do %>
<table>
<thead>
<tr>
<th>Checkbox</th>
<th>Date</th>
<th>Mailer ID</th>
<th>Payment amt</th>
<th>Transaction type</th>
<th>Transaction</th>
<th>Deposit</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #payments.each do |payment| %>
<tr>
<td><%= check_box_tag "payment_id[]", payment.id, checked = false %></td>
<td><%= payment.created_at %></td>
<td><%= payment.mailer_id %></td>
<td><%= number_to_currency(payment.payment_amt) %></td>
<td><%= payment.transaction_type %></td>
<td><%= payment.transaction_id %></td>
<td><%= payment.deposit_id %></td>
<td><%= payment.user_id %></td>
<td><%= link_to 'Show', payment %></td>
<td><%= link_to 'Edit', edit_payment_path(payment) %></td>
<td><%= link_to 'Destroy', payment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Edit Checked" %>
<% end %>
thanks for the help
did you notice in your code?
<% form_tag deposit_checks_path :method => :put do %>
you forgot to add a comma. It should have been
<% form_tag deposit_checks_path, :method => :put do %>
otherwise the method: :put is appended to the action attribute
Main Point:
You forgot to put an equals sign before form_tag
Try this
<%= form_tag deposit_checks_path, :method => :put do %>
Try like this
<table>
<thead>
<tr>
<th>Checkbox</th>
<th>Date</th>
<th>Mailer ID</th>
..
..
</tr>
</thead>
<tbody>
<tr>
<td>
<% form_tag deposit_checks_path :method => :put do %>
<table>
<% #payments.each do |payment| %>
<tr>
<td><%= check_box_tag "payment_id[]", payment.id, checked = false %></td>
<td><%= payment.created_at %></td>
<td><%= payment.mailer_id %></td>
...
...
</tr>
<%end%>
</table>
<% end %>
</td>
</tr>
</tbody>
</table>

Capybara check if no selector has text of numbers greater than 500

I want to check if there are 0 instances of numbers greater than 500 in all of the rows of a table.
The table looks like this:
<% #trips.each do |trip| %>
<tr class="trip-row" data-categories= "<%= #categories.map(&:name).join(":") %>">
<td id="ranking"><%= #trips.index(trip) + 1 %></td>
<td id="trip-title"> <%= trip.title %> </td>
<td><%= trip.departure_city %></td>
<td><%= trip.arrival_city %></td>
<td><%= trip.departure_airline %></td>
<td><%= trip.short_description %></td>
<td><%= trip.departure_date.strftime("%b %d, %Y, %I:%M") %></td>
<td><%= trip.return_date.strftime("%b %d, %Y, %I:%M") %></td>
<td><%= trip.price %></td>
<td><%= link_to "Details", real_trip_path(trip, info: { departure_date: trip.departure_date, return_date: trip.return_date, price: trip.price }), class: "btn btn-info" %></td>
</tr>
<% end %>
Can I write this:
within all(".trip-row") do
refute page has.content? #number greater than 500?
end
You can evaluate values in each cell rather than row by row.
page.all(.'trip-row td').each do |c|
#evaluate here
end
You'll probably want to use regex to pull the numbers out from each cell, then evaluate from there using an rspec expectation.

How to display a list based on date in rails?

I'm trying to build a real simple event listing app that list a bunch of event based on Dates. I'm fairly new and was wondering how would I display a list or table based on dates? This is what I have now.
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Link</th>
<th>Date</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #events.each do |event| %>
<tr>
<td><%= event.title %></td>
<td><%= event.description %></td>
<td><%= event.link %></td>
<td><%= event.date %></td>
<td><%= link_to 'Show', event %></td>
<td><%= link_to 'Edit', edit_event_path(event) %></td>
<td><%= link_to 'Destroy', event, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
I would like it display in a date format. For example all events under the date of February/21 will appear under that specific heading.
Thanks!
You can try something like this:
<% #events.group_by(&:date).each do |date, events| %>
<tr><td colspan="7"><h1><%= date %></h1></td></tr>
<% events.each do |event| %>
<tr>
<td><%= event.title %></td>
<td><%= event.description %></td>
<td><%= event.link %></td>
<td><%= event.date %></td>
<td><%= link_to 'Show', event %></td>
<td><%= link_to 'Edit', edit_event_path(event) %></td>
<td><%= link_to 'Destroy', event, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<% end %>

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

Rails 3. Get totals from array

I'm printing out a table with line totals but I also want to get the grand total of the columns. This following code doesn't work, instead of the grand totals it just prints out the values of the last iteration.
<% #shipments.each do |shipment| %>
<tr>
<td style="text-align:center;"><%= shipment.file_number %></td>
<td><%= shipment.shipper.company_name %></td>
<td><%= shipment.hbl %></td>
<td><%= shipment.status %></td>
<td><%= shipment.age %></td>
<td><%= shipment.invoice.read_issued_at unless shipment.invoice.nil? %></td>
<td><%= number_to_currency shipment.invoice.customer_total unless shipment.invoice.nil? %></td>
<td><%= number_to_currency shipment.invoice.customer_amount_paid unless shipment.invoice.nil? %></td>
<td><%= number_to_currency shipment.invoice.customer_open_balance unless shipment.invoice.nil? %></td>
</tr>
<%
grand_customer_total = 0
grand_customer_amount_paid = 0
grand_customer_open_balance = 0
grand_customer_total += shipment.invoice.customer_total
grand_customer_amount_paid += shipment.invoice.customer_amount_paid
grand_customer_open_balance += shipment.invoice.customer_open_balance
%>
<% if #shipments.last == shipment %>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<th>Totals</th>
<td><%= number_to_currency grand_customer_total %></td>
<td><%= number_to_currency grand_customer_amount_paid %></td>
<td><%= number_to_currency grand_customer_open_balance %></td>
</tr>
<% end %>
The reason your code isn't working is that your variables are defined in the block, so they're considered block-local variables. Once the block is exited those variables are marked to be cleared; each iteration re-defines those variables. It also doesn't help that you're reassigning them to 0 on each iteration, but that isn't even coming into effect here because the variables aren't defined each time through.
You could simply define the variables before the block, but that's still pretty messy. Since Ruby idioms and conventions put an emphasis on clean and well-organized code, I'd stray away from that and instead calculate these numbers separately, possibly in your controller.
#totals = {
:overall => #shipments.reduce(0) { |total, shipment| total + shipment.invoice.customer_total },
:paid => #shipments.reduce(0) { |total, shipment| total + shipment.invoice.customer_amount_paid },
:balance => #shipments.reduce(0) { |total, shipment| total + shipment.invoice.customer_open_balance }
}
Then, instead of using #shipments.last comparison, you can just do the following after your shipments table output:
<tr>
<td colspan="5"></td>
<th>Totals</th>
<td><%= number_to_currency #totals[:overall] %></td>
<td><%= number_to_currency #totals[:paid] %></td>
<td><%= number_to_currency #totals[:balance] %></td>
</tr>
You set the grand total to zero every time through the loop. move the initialization up before the loop.
you can use ruby inject
something like
#shipments.inject(0) { |sum, shipment| sum + shipment.invoice.customer_total }
or to keep with your code layout, just initialize the grand_customer_* objects outside of the #shipments.each loop because you are resetting them every time through
<%
grand_customer_total = 0
grand_customer_amount_paid = 0
grand_customer_open_balance = 0
%>
<% #shipments.each do |shipment| %>
<tr>
<td style="text-align:center;"><%= shipment.file_number %></td>
<td><%= shipment.shipper.company_name %></td>
<td><%= shipment.hbl %></td>
<td><%= shipment.status %></td>
<td><%= shipment.age %></td>
<td><%= shipment.invoice.read_issued_at unless shipment.invoice.nil? %></td>
<td><%= number_to_currency shipment.invoice.customer_total unless shipment.invoice.nil? %></td>
<td><%= number_to_currency shipment.invoice.customer_amount_paid unless shipment.invoice.nil? %></td>
<td><%= number_to_currency shipment.invoice.customer_open_balance unless shipment.invoice.nil? %></td>
</tr>
<%
grand_customer_total += shipment.invoice.customer_total
grand_customer_amount_paid += shipment.invoice.customer_amount_paid
grand_customer_open_balance += shipment.invoice.customer_open_balance
%>
<% if #shipments.last == shipment %>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<th>Totals</th>
<td><%= number_to_currency grand_customer_total %></td>
<td><%= number_to_currency grand_customer_amount_paid %></td>
<td><%= number_to_currency grand_customer_open_balance %></td>
</tr>
<% end %>

Resources