Rails create new records based on checked records - ruby-on-rails

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

Related

Iterate in array of hashes in side array in ruby

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

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.

rails update with checkbox

I want in my restaurant application to change the status of "preparated" to "deliverated" with a checkbox list but when doing the reload of the view where it does not make any changes, and logically the query is well I do not understand which is the problem.
View of the application
Orderdish.rb
class Orderdish < ApplicationRecord
belongs_to :order
belongs_to :dish
def self.orders_waiter waiter
orders_waiter=Orderdish.all.joins({:order => :table},:dish).select("orderdishes.*,orderdishes.state as status,orderdishes.id as id, dishes.*, dishes.speciality_id as speciality_id, tables.*, orders.*, orders.created_at as order_created, tables.*, tables.waiter_id as waiter_id").where("waiter_id = ?", waiter.id).where("orderdishes.state = ?", "preparated").order("order_created ASC")
return orders_waiter
end
end
Controller
def orderWaiter
#ordersWaiter = Orderdish.orders_waiter current_waiter
#ordersWaiter = #ordersWaiter.paginate(:page => 1, :per_page => 12)
end
def deliverated
puts Orderdish.where(id: params[:orderdishes_ids]).update_all state: "deliverated"
redirect_to :orderWaiter
end
Routes
resources :order_views do
collection do
put :preparated
put :deliverated
end
end
get 'orderWaiter' => 'order_views#orderWaiter'
View
<div class="aboutUs">
<%= form_tag deliverated_order_views_path, method: :put do %>
<table class="table table-inverse">
<thead>
<tr>
<th> </th>
<th>Created </th>
<th>Table </th>
<th>Name</th>
<th>Specification</th>
<th>Quantity</th>
<th>State</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #ordersWaiter.each do |orderwaiter| %>
<tr>
<td><%= check_box_tag "orderdishes_ids[]", orderwaiter.id %></td>
<td><%= orderwaiter.order_created.to_time.strftime('%B %e at %l:%M %p') %></td>
<td><%= orderwaiter.number %></td>
<td><%= orderwaiter.name %></td>
<td><%= orderwaiter.specification %></td>
<td><%= orderwaiter.quantity %></td>
<td><%= orderwaiter.status %></td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Deliverated" %>
</div>
<% end %>

how do i display an index and a form on my show page?

How do I have my User's (users model) show page display a list of the Serivces (another model) they are offering AND also a form where people can book Appointments (another model) for their Services?
It looks like I need to make the #services variable more flexible in my controller. But I'm not sure how. It seems like only one partial I am rendering is able to make use of it in my controller. Weird!
USERS CONTROLLER
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#appointment = Appointment.new
#services = #user.services.collect {|service| [ service.id, service.title, service.description, service.length, service.user_id] }
end
end
USERS/SHOW.HTML.ERB:
LIST OF SERVICES PARTIAL: <% render 'shared/services_list' %>
<table>
<thead>
<tr>
<th>Start time</th>
<th>End time</th>
<th>Note</th>
<th>Service</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #appointments.each do |appointment| %>
<tr>
<td><%= appointment.start_time %></td>
<td><%= appointment.end_time %></td>
<td><%= appointment.note %></td>
<td><%= appointment.service.title %></td>
<td><%= link_to 'Show', appointment %></td>
<td><%= link_to 'Edit', edit_appointment_path(appointment) %></td>
<td><%= link_to 'Destroy', appointment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
USERS/SHOW.HTML.ERB:
APPOINTMENTS FORM PARTIAL: <%= render 'appointments/form' %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Length</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #services.each do |service| %>
<tr>
<td><%= service.title %></td>
<td><%= service.description %></td>
<td><%= service.length %></td>
<td><%= service.user.email %></td>
<td><%= link_to 'Show', service %></td>
<td><%= link_to 'Edit', edit_service_path(service) %></td>
<td><%= link_to 'Destroy', service, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
ERROR:
NoMethodError in Users#show
undefined method `title' for [1, "exmaple title", "example description", 90, 1]:Array
Line 15 is highlighted -> <%= service.title %>
<tbody>
<% #services.each do |service| %>
<tr>
<td><%= service.title %></td>
<td><%= service.description %></td>
<td><%= service.length %></td>
<td><%= service.user.email %></td>
Thanks!
Okay. #peshkira was right. I neeeded to call the object.
So I added this line to my controller #services_list = #user.services.collect so I could create a list in the style of an 'index'.
My full controller looks like this.
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#appointment = Appointment.new
#services = #user.services.collect {|service| [ service.title, service.id] }
#services_list = #user.services.collect
end
end
That will supply functionality for a nested form(A Service & an Appointment for that Service) and also pull the data for a list of Services.

Using a if statement in a looped table rails

I have two models, one with cards and another with their associated rewards programs. I'm showing all of them in a table with a conditional if statement in some of the columns but I can't figure out why an if else statement screws up my columns. I posted two examples one that works and one that doesn't. I need the second one to work to add some additional functionality
This example works
<table>
<tr>
<th>Card</th>
<th>General Rewards</th>
<th>Gas Amount</th>
<th>Movies Amount</th>
<th>Museums Amount</th>
<th>Theme Park Amount</th>
<th>Restaurant Amount</th>
<th>Department Store Amount</th>
</tr>
<% #cards.each do |card| %>
<tr>
<td><%= card.name %></td>
<td><%= card.general_rate %> </td>
<% card.rewards.each do |category| %>
<% if category.name.downcase == "gas" %>
<td><%= category.threshold_check(#gas) %></td>
<% end %>
<% if category.name.downcase == "movies" %>
<td><%= category.threshold_check(#movies) %></td>
<% end %>
<% if category.name.downcase == "museums" %>
<td><%= category.threshold_check(#museums) %></td>
<% end %>
<% if category.name.downcase == "theme parks" %>
<td><%= category.threshold_check(#theme_parks) %></td>
<% end %>
<% if category.name.downcase == "restaurants" %>
<td><%= category.threshold_check(#restaurants) %></td>
<% end %>
<% if category.name.downcase == "department stores" %>
<td><%= category.threshold_check(#department_stores) %></td>
<% end %>
<% end %>
</tr>
<% end %>
</table>
This adds extra columns to the end
<table>
<tr>
<th>Card</th>
<th>General Rewards</th>
<th>Gas Amount</th>
<th>Movies Amount</th>
<th>Museums Amount</th>
<th>Theme Park Amount</th>
<th>Restaurant Amount</th>
<th>Department Store Amount</th>
</tr>
<% #cards.each do |card| %>
<tr>
<td><%= card.name %></td>
<td><%= card.general_rate %> </td>
<% card.rewards.each do |category| %>
<td><%= category.name.downcase == "gas" ? category.threshold_check(#gas) : 0 %></td>
<td><%= category.name.downcase == "movies" ? category.threshold_check(#movies) : 0 %></td>
<td><%= category.name.downcase == "museums" ? category.threshold_check(#museums) : 0 %></td>
<td><%= category.name.downcase == "theme parks" ? category.threshold_check(#theme_parks) : 0 %></td>
<td><%= category.name.downcase == "restaurants" ? category.threshold_check(#restaurants) : 0 %></td>
<td><%= category.name.downcase == "department stores" ? category.threshold_check(#department_stores) : 0 %></td>
<% end %>
</tr>
<% end %>
</table>
If you will notice the main difference between your 2 versions is that the first one if your conditionals dont comply, you will not issue a <td></td>
On the other hand your second version does have <td></td> no matter if the conditionals are true or not.
Maybe that could be the source for your extra columns.
P.S. In your first example I think you have a typo but <%= category.threshold_check(#movies) %> doesn't have an opening <td> and it's closing </td> is outside the if statement.

Resources