rails update with checkbox - ruby-on-rails

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

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>

How to show edit button only for a record and save data to database from edit form in rails

I am displaying all the records from db in the form of HTML table along with edit button. On, click of edit button it redirects to edit view which will display the additional columns.
Here, I was showing save button in edit form for all the rows/records. Is there a way to prevent so that I can just show to the one I selected in index form.
Also, after updating the fields how do I save the record and redirect to index view.
Please find the code snippets below:
metrics_controller.rb:
class MetricsController < ApplicationController
def index
#metricAll = Metric.all
end
def show
#metric = Metric.find(params[:id])
end
def edit
#metric = Metric.find(params[:id])
#metricAll = Metric.all
end
def create
#metric = Metric.new(post_params)
if(#metric.save)
redirect_to #metric
else
render 'new'
end
end
def update
#metric = Metric.find(params[:id])
if(#metric.update_attributes(post_params))
redirect_to #metric
else
render 'edit'
end
end
private def post_params
params.require(:metric).permit(:Metric, :WI, :Value, :UT, :Score, :IsValid, :UserName, :Comments)
end
end
edit.html.rb:
<%= form_for :metrics_controller, url: metric_path(#metric), method: :patch do |f| %>
<table id="metrics">
<thead>
<tr id="AllMetricColumnNames">
<th id="Metric"><div>Metric</th>
<th id="WI">WI</th>
<th id="Value">Value</th>
<th id="UT">UT</th>
<th id="Score">Score</th>
<th id="IsValid">IsValid</th>
<th id="UserName">UserName</th>
<th id="Comments">Comments</th>
<th id="EditColumn">Edit</th>
</tr>
</thead>
<% #metricAll.each do |data| %>
<tr id="AllMetricValues">
<td id="Value"><%= data.Metric %></td>
<td id="WI"><%= data.WI %></td>
<td id="Value"><%= data.Value %></td>
<td id="UT"><%= data.UT %></td>
<td id="Score"><%= data.Score %></td>
<td><%= f.select :IsValid, options_for_select(['True', 'False']), :include_blank => true, :class => 'chosen-select', :required => true, value: data.IsValid %></td>
<td id="UserName"><%= data.UserName %></td>
<td id="Comments"><%= f.text_field :Comments, value: data.Comments %></td>
<td id="SaveButton"><%= f.submit "Save" %></td>
<% end %>
</tr>
</table>
<% end %>
Screenshot:
You need to have a form for each metric:
<table id="metrics">
<thead>
<tr id="AllMetricColumnNames">
<th id="Metric"><div>Metric</th>
<th id="WI">WI</th>
<th id="Value">Value</th>
<th id="UT">UT</th>
<th id="Score">Score</th>
<th id="IsValid">IsValid</th>
<th id="UserName">UserName</th>
<th id="Comments">Comments</th>
<th id="EditColumn">Edit</th>
</tr>
</thead>
<% #metricAll.each do |data| %>
<%= form_for :metrics_controller, url: metric_path(data), method: :patch do |f| %>
<tr id="AllMetricValues">
<td id="Value"><%= data.Metric %></td>
<td id="WI"><%= data.WI %></td>
<td id="Value"><%= data.Value %></td>
<td id="UT"><%= data.UT %></td>
<td id="Score"><%= data.Score %></td>
<td><%= f.select :IsValid, options_for_select(['True', 'False']), :include_blank => true, :class => 'chosen-select', :required => true, value: data.IsValid %></td>
<td id="UserName"><%= data.UserName %></td>
<td id="Comments"><%= f.text_field :Comments, value: data.Comments %></td>
<td id="SaveButton"><%= f.submit "Save" %></td>
</tr>
<% end %>
<% end %>
</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.

how to render view/index in another view/index?

I dont understand how to insert code into my welcome/index.html.erb file. Here is my code:
Welcomes_controller.rb
def index
#welcomes = Welcome.all
end
Schedules_controller.rb
def index
#schedules = Schedule.all
end
schedules/index.html.erb
<table class="table table-hover">
<thead>
<tr>
<th>Время<br>отправления</th>
<th>Город</th>
<th>Место<br> отправления</th>
<th>Время <br>прибытия</th>
</tr>
</thead>
<tbody>
<% #schedules.each do |s| %>
<tr>
<td><%= s.deptime %></td>
<td><%= s.city %></td>
<td><%= s.street %></td>
<td><%= s.aparttime %></td>
</tr>
<% end %>
</tbody>
</table>
How do I insert that code into welcome/index.html.erb?
To render the schedules index template from the welcomes_controller.rb
def index
#welcomes = Welcome.all
render "/schedules/index"
end
However, this will present a problem because the table in the view depends on an #schedules instance variable to be set and it will return nil because it is not assigned a value in the controller.
you might want to do this:
def index
#schedules = Welcome.all
render "/schedules/index"
end
Which does not really make sense to me from a semantic point of view. You might want to rename the instance variable to something more model agnostic.
In the other answer it was suggested to use a partial. That could actually be a better solution depending on the use case.
create a partial app/views/shared/_index_table.html.erb with the content
<table class="table table-hover">
<thead>
<tr>
<th>Время<br>отправления</th>
<th>Город</th>
<th>Место<br> отправления</th>
<th>Время <br>прибытия</th>
</tr>
</thead>
<tbody>
<% schedules.each do |s| %>
<tr>
<td><%= s.deptime %></td>
<td><%= s.city %></td>
<td><%= s.street %></td>
<td><%= s.aparttime %></td>
</tr>
<% end %>
</tbody>
</table>
Then, app/views/schedules/index.html.erb
<h1>Schedules</h1>
<%= render partial: "shared/index_table", :locals => { schedules: #welcomes } %>
and app/views/welcomes/index.html.erb
<h1>Welcome</h1>
<%= render partial: "shared/index_table", :locals => { schedules: #schedules } %>
This works only if you have the same set of attributes (deptime, city, street, aparttime) on both Welcome and Schedule models

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