date range not working - ruby-on-rails

I'm having two issues with the same aspect of my application, the abstract view is this:
The app allows users to view different video clips. On the back-end/admin section the admin can track the views of any particular group of clips. For example, the admin can choose Universal Studios, select a date range: Beginning Date: 2015-01-1, End Date: 2016-01-1 and hit filter, and see the number of total views that Universal received within this range.
The problem is, for one, datepicker is no longer working, so when I click on the date fields, no calendar appears, I manually have to type in the date. And for two, I'm only getting results between furthers: 2015-10-18 and 2016-02-18. When I type in 2015-09-18 or anything earlier it doesn't generate results.
See the code below, and let me know if you need any other code!
Index.html.erb snippit:
<tr>
<td><%= f.text_field :start_date, value: 1.month.ago.strftime('%Y-%m-%d') %></td>
<td><%= f.text_field :end_date, value: Time.now.strftime('%Y-%m-%d') %></td>
<td>
<%= f.submit "Filter", class: "btn btn-danger", data: {disable_with: 'Searching...'} %>
<%= link_to 'Clear Form', '#', id: "clear", class: 'btn btn-default' %>
</td>
</tr>
_impressions.html.erb snippit:
</style>
<div class="clearfix"></div>
<% if #impressions.blank? -%>
<div class="span12"><h1>Nothing found</h1></div>
<% else %>
<div class="alert alert-success">
<div class="">Total Views: <b><%= #impressions.count %></b></div>
<div class="">Percentage to overall views: <b><%= #percentage.round(2) %>%</b></div>
</div>
<table id="table_3" class="table table_output">
<thead>
<tr>
<td class="stat_head">Studio</td>
<td class="stat_head">Film</td>
<td class="stat_head">Clip</td>
<td class="stat_head">User</td>
<td class="stat_head">State</td>
<td class="stat_head">Country</td>
<td class="stat_head">Type</td>
<td class="stat_head">Date</td>
</tr>
</thead>
<tbody>
<% #impressions.each do |impression| -%>
<% #clip = Clip.where(id: impression.impressionable_id).first %>
<% user = User.where(id: impression.user_id).first %>
<tr>
<td><%= link_to #clip.film.studio.name, saas_admin_studio_path(#clip.film.studio) %></td>
<td><%= link_to #clip.film.name, saas_admin_studio_film_path(#clip.film.studio, #clip.film) %></td>
<td><%= link_to #clip.name, saas_admin_studio_film_clips_path(#clip.film.studio ,#clip.film) %></td>
<td><%= user.present? ? (link_to user.username, saas_admin_account_user_path(user.account, user)) : ' - ' %></td>
<td><%= user.present? ? user.account.organization.state : ' - ' %></td>
<td><%= user.present? ? user.account.organization.country_id : ' - ' %></td>
<td><%= user.present? ? user.account.subscription.state : ' - ' %></td>
<td><%= user.present? ? impression.created_at.strftime("%b %e, %Y %l:%M%P") : ' - ' %></td>
</tr>
<% end -%>

Why don't you do something like this :
<%=label_tag :start_date, "Start Date" %>
<input id="datepicker" name="someName" ...any other options>
then :
<script>
$(document).ready(function(){
$("#datepicker").datepicker();
});
</script>
then of course add a similar field for end_date as well.
Note that you must include //= require jquery-ui/datepicker in your application.js

Related

how to set multiple default form values on submission in rails?

attendance table that takes has attribute status, date,recuritment_id, and project_site_id .
project_site has one_to_many association with attendance
recuritmenthas one_to_many association with attendance.
i am taking attribute from recruitment table when attribute status is joined in attendance#form view.
<% (1..(Time.days_in_month #project_site.attendance_month.strftime("%m").to_i)).each do |date| %>
here #project_site.attendance_month contains the attendance month value. based on month i calculate number of days column along with name from recuritmnet table.
here is view-
holydays master contains corresponding month holyday's date and in view it auto match date and prints "H",
All input default selected as P. there is final submission_button that chnages boolean attribute. now on final submission i want to push all default selected P into attendance table.
attendance_controller.rb
def new
if #project_site.submission_status == false
#attendance = Attendance.new
#date = HolydayCalendar.all
#recruitment = Recruitment.all.where(current_status: "2")
else
redirect_to project_site_attendances_path
end
end
from.html.erb (attendance controller view)
<table>
<thead>
<tr>
<th class="attendance-emp-name">Emp. Name</th>
<% (1..(Time.days_in_month #project_site.attendance_month.strftime("%m").to_i)).each do |date| %>
<th class="text-center"><%= date %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #recruitment.where(location: #project_site.site_id).each do |recruitment| %>
<tr>
<td class="attendance-emp-name"><%= recruitment.name %></td>
<% (1..(Time.days_in_month #project_site.attendance_month.strftime("%m").to_i)).each do |date| %>
<%= form_with(model: attendance, :html => {:id => 'attendance-form-validation'}, url:[#project_site, #attendance], local: true) do |f| %>
<% if HolydayCalendar.find_by(date: (#project_site.attendance_month.strftime("%Y-%m")+"-"+date.to_s), total_site_id: #project_site.site_id)%>
<td class="holyday text-center"><%= "H" %></td>
<% elsif recruitment.attendances.find_by(attendance_date: (#project_site.attendance_month.strftime("%Y-%m")+"-"+date.to_s)) == nil %>
<td>
<%= f.select :status, [['P', 1], ['A', 2], ['L', 4], ['WE', 5], ['CO', 6]], {}, { onchange: 'this.form.submit()', class: 'attendance-select-input' } %>
</td>
<% else %>
<% attendance_value = recruitment.attendances.find_by(attendance_date: (#project_site.attendance_month.strftime("%Y-%m")+"-"+date.to_s)) %>
<%if attendance_value.status == 1 %>
<td class="presant text-center"><%="P" %></td>
<% elsif attendance_value.status == 2 %>
<td class="absent text-center"><%="A" %></td>
<%elsif attendance_value.status == 3 %>
<td class="holyday text-center"><%="H" %></td>
<%elsif attendance_value.status == 4 %>
<td class="leave text-center"><%= "L" %></td>
<%elsif attendance_value.status == 5 %>
<td class="weekend text-center"><%= "WE" %></td>
<%elsif attendance_value.status == 6 %>
<td class="compoff text-center"><%= "CO" %></td>
<% end %>
<% end %>
<%= f.hidden_field :attendance_date, value: (#project_site.attendance_month.strftime("%Y-%m")+"-"+date.to_s)%>
<%=f.hidden_field :recruitment_id, value: recruitment.id%>
<%=f.hidden_field :project_site_id, value: #project_site.id%>
<% end %>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<% if #project_site.submission_status == true %>
<div class="text-center">
<%= link_to "Submit Attendance", set_submission_status_project_site_path(#project_site), method: :put, data: { confirm: 'Make Sure you marked all attendance before submission' }, :class=>"button primary disabled" %>
</div>
<% else %>
<div class="text-center">
<%= link_to "Submit Attendance", set_submission_status_project_site_path(#project_site), method: :put, data: { confirm: 'Make Sure you marked all attendance before submission' }, :class=>"button primary" %>
</div>
<% end %>
project_sites_controller.rb
def set_submission_status
#project_site = ProjectSite.find(params[:id])
#project_site.update(submission_status: true)
end
It's a little unclear what your question is, but I gather it has to do with your form sending back an array op options.
If you want your html form to return an array of information, then you need to indicate it in your html using the name = "attendance[]" syntax, notice the square brackets.
Check out this article on the subject https://mattstauffer.com/blog/a-little-trick-for-grouping-fields-in-an-html-form/

How to setup restriction for view in a table tag?

This is code of my view file, it shows multiple columns for the table.
The restriction is working but columns are multiple.
<table class="table table-responsive">
<tr>
<th>Title</th>
<th>Description</th>
<% obj.each do |article| %>
<% if logged_in? && current_user == article.user %>
<th>Edit</th>
<th>Show</th>
<th>Delete</th>
<th>Created By</th>
<th>Created At</th>
<th>Updated At</th>
<% end %>
<% end %>
</tr>
<% obj.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.description %></td>
<% if logged_in? && current_user == article.user %>
<td><%= link_to "Edit", edit_article_path(article), class: "btn btn-primary" %> </td>
<td><%= link_to "show", article_path(article), class: "btn btn-success" %></td>
<td><%= link_to "Delete", article_path(article), method: :delete, data: {confirm: "Are you Sure?"}, class: "btn btn-danger" %></td>
<td> <%= article.user.username if article.user %> </td>
<td> <%= time_ago_in_words(article.created_at) %> ago.</td>
<td> <%= time_ago_in_words(article.updated_at) %> ago.</td>
</tr>
<% end %>
<% end %>
</table>
<%= link_to 'Back', root_path, class: "btn btn-primary btn-lg" %>
You're printing the thead depending on the value of obj, try leaving just the second iteration, to print each tr, maybe something like:
<% if logged_in? && current_user == article.user %>
<tr>
<th>Edit</th>
<th>Show</th>
<th>Delete</th>
<th>Created By</th>
<th>Created At</th>
<th>Updated At</th>
</tr>
<tbody>
<% obj.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.description %></td>
<% if logged_in? && current_user == article.user %>
<td><%= link_to "Edit", edit_article_path(article), class: "btn btn-primary" %> </td>
<td><%= link_to "show", article_path(article), class: "btn btn-success" %></td>
<td><%= link_to "Delete", article_path(article), method: :delete, data: {confirm: "Are you Sure?"}, class: "btn btn-danger" %></td>
<td> <%= article.user.username if article.user %> </td>
<td> <%= time_ago_in_words(article.created_at) %> ago.</td>
<td> <%= time_ago_in_words(article.updated_at) %> ago.</td>
<% end %>
</tr>
<% end %>
</tbody>
<% end %>
What I understand by your title question, you want to prevent the view of all your table . So
<% if logged_in? && current_user == article.user %>
<table class="table table-responsive">
<tr>
...
the rest of the code for show the table
....
</table>
<% end %>
This will hide the view of the whole table and It only will be shown for the article author
The problem is you're iterating each article in obj and outputting a header each time the currently logged in user is the articles user:
<% obj.each do |article| %>
<% if logged_in? && current_user == article.user %>
<th>Edit</th>
<th>Show</th>
<th>Delete</th>
<th>Created By</th>
<th>Created At</th>
<th>Updated At</th>
<% end %>
<% end %>
You probably only want to show the header a single time if the user is logged in and is the user of any article, Ruby has a method for that, Enumerable#any?. So something like this should be what you're looking for:
<% if logged_in? && obj.any? { |article| current_user == article.user } %>
<th>Edit</th>
<th>Show</th>
<th>Delete</th>
<th>Created By</th>
<th>Created At</th>
<th>Updated At</th>
<% end %>
This should only output one set of the headers if the user is logged in and is the author of any article on the page, instead of one set of headers per article authored.

Form_tag do not add check_box_tag value into parameters

I have Pieces model, which is store item's parts. When I need, I'm asking to users, check pieces is ok or nok with generated forms.
I am creating reports model to store the forms which I asked from users.
So, when I generate the forms check_box and and text_field works fine. But when I submit the form, I can't get check_box values as a parameter.
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"5OmdXCDixwxru2yBtB3YmT3YF/ZqOByoAJ3J29TyJikxJdA+RXAvGjwu8MBIPlJCROFx4V7AUcA7IIqIgthD9g==",
"notes"=>{"79"=>"asdasda","84"=>""},
"commit"=>"Submit",
"id"=>"5"}
here is my edit.html.erb
<%- model_class = Report -%>
<div class="page-header">
<h1><%=t '.title', :default => [:'helpers.titles.edit', 'Edit %{model}'], :model => model_class.model_name.human.titleize %></h1>
</div>
<% if #report.errors.any? %>
<div id="error_expl" class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><%= pluralize(#report.errors.count, "error") %> prohibited this report from being saved:</h3>
</div>
<div class="panel-body">
<ul>
<% #report.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
<table class="table table-striped">
<thead>
<tr>
<th><%= model_class.human_attribute_name(:status) %></th>
<th><%= model_class.human_attribute_name(:serial_no) %></th>
<th><%= model_class.human_attribute_name(:category) %></th>
<th><%= model_class.human_attribute_name(:name) %></th>
<th><%= model_class.human_attribute_name(:brand) %></th>
<th><%= model_class.human_attribute_name(:model) %></th>
<th><%= model_class.human_attribute_name(:quantity) %></th>
</tr>
</thead>
<tbody>
<%= form_tag(report_path(#report), :method => :patch) do %>
<% #pieces.each do |piece| %>
<tr>
<td><div class="form-group">
<%= check_box_tag(:status, name: "status["+piece.id.to_s+"]", class: 'form-control') %>
</div></td>
<td><%= piece.try(:serial_no) %></td>
<td><%= piece.item.category.name %></td>
<td><%= piece.item.name %></td>
<td><%= piece.item.brand %></td>
<td><%= piece.item.model %></td>
<% if piece.serial_no.start_with?("D") %>
<td><%= piece.item.pieces.count %> <%= piece.item.unit.name %></td>
<% else %>
<td><%= piece.item.pieces.sum(:quantity) %> <%= piece.item.unit.name %></td>
<% end %>
</tr>
<tr>
<td colspan=7 class="hidden" id="hebele">
<%= text_field_tag(:note,"", placeholder: "Note ", name: "notes["+piece.id.to_s+"]",class: "form-control ") %>
</td>
</tr>
<% end %>
</tbody>
</table>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<%= submit_tag("Submit",:class => 'btn btn-primary') %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")), reports_path, :class => 'btn btn-default' %>
</div>
</div>
<% end %>
<script type="text/javascript">
$("[id='status']").bootstrapSwitch({
size: 'mini',
onColor: 'success',
offColor: 'danger',
indeterminate: true,
onSwitchChange: function(event, state) {
if (state == false){
$("#hebele").removeClass('hidden');
}else if(state == true){
$("#hebele").addClass('hidden');
}
}
});
</script>
controller:
def edit
#pieces = #report.pieces.joins(:item).group(:item_id)
#report = Report.find(params[:id])
end
You need to define the value of the check_box_tag:
<%= check_box_tag :status, piece.id, class: 'form-control' %>
If you're editing a #report, you'll be best using form_for:
<table>
<tbody>
<%= form_for #report do |f| %>
<% #pieces.each do |piece| %>
<tr>
<td>
<div class="form-group">
<%= f.check_box :status, class: 'form-control', price.id %>
</div>
</td>
<td><%= piece.try(:serial_no) %></td>
<td><%= piece.item.category.name %></td>
<td><%= piece.item.name %></td>
<td><%= piece.item.brand %></td>
<td><%= piece.item.model %></td>
<% if piece.serial_no.start_with?("D") %>
<td><%= piece.item.pieces.count %> <%= piece.item.unit.name %></td>
<% else %>
<td><%= piece.item.pieces.sum(:quantity) %> <%= piece.item.unit.name %></td>
<% end %>
</tr>
<tr>
<td colspan=7 class="hidden" id="hebele">
<%= f.text_field :note, placeholder: "Note", class: "form-control" %>
</td>
</tr>
<% end %>
<tr><td colspan="7"><%= f.submit, class: "btn btn-primary" %></td></tr>
<% end %>
</tbody>
</table>

How to change values which get from check box into SQL condition?

I use check_box_tag to get selected beacon, and i to use collection_select to define a value. when user press the button, I want to all selected beacons change value into what collection_select choose, but I don't know how to use ids[] to set my condition like "where...in ..."
here is my action, I want params[:ids] change in to just like [1,2,3]
#beacons = Beacon.where(:id => params[:ids]).update_all(:installer_id => params[:account][:installer_id])
here is my table:
<table class="col-md-12 table-bordered table-striped table-condensed cf">
<thead class="cf">
<th>Check</th>
<th>BeaconUUID</th>
<th>BeaconCategory</th>
<th>BeaconLocate</th>
<th>BeaconName</th>
<th>BeaconAddress</th>
<th>Assign</th>
</thead>
<tbody>
<% #beacons.each do |beacon| %>
<tr>
<td><%= check_box_tag "ids[]", beacon.id %></td>
<td><%= beacon.beacon_uuid %></td>
<td><%= beacon.cid %></td>
<td><%= beacon.lid %></td>
<td><%= beacon.beacon_name %></td>
<td><%= beacon.beacon_address %></td>
<% if beacon.installer_id.nil? %>
<td>not assign</td>
<% else %>
<td>assigned:<%= beacon.installer_id %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<%= form_tag assigned_beacons_path, :method => 'post' do %>
<td><%= collection_select(:account, :installer_id, Account.where('manager_id = ?',session[:user_id]), :id, :email, {:prompt => 'choose!'}, :style => "width: 100px;") %></td>
<td><p data-placement="top" data-toggle="tooltip" title="Edit"><%= submit_tag 'assign', class: "btn btn-info btn-xs"%></p></td>
<% end %>
As was pointed out in comments, check_box_tag should be moved inside of form_tag block:
<%= form_tag assigned_beacons_path, :method => 'post' do %>
<table class="col-md-12 table-bordered table-striped table-condensed cf">
<thead class="cf">
<th>Check</th>
<th>BeaconUUID</th>
<th>BeaconCategory</th>
<th>BeaconLocate</th>
<th>BeaconName</th>
<th>BeaconAddress</th>
<th>Assign</th>
</thead>
<tbody>
<% #beacons.each do |beacon| %>
<tr>
<td><%= check_box_tag "ids[]", beacon.id %></td>
<td><%= beacon.beacon_uuid %></td>
<td><%= beacon.cid %></td>
<td><%= beacon.lid %></td>
<td><%= beacon.beacon_name %></td>
<td><%= beacon.beacon_address %></td>
<% if beacon.installer_id.nil? %>
<td>not assign</td>
<% else %>
<td>assigned:<%= beacon.installer_id %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<td><%= collection_select(:account, :installer_id, Account.where('manager_id = ?',session[:user_id]), :id, :email, {:prompt => 'choose!'}, :style => "width: 100px;") %></td>
<td><p data-placement="top" data-toggle="tooltip" title="Edit"><%= submit_tag 'assign', class: "btn btn-info btn-xs"%></p></td>
<% end %>
Also, dangling <td> blocks don't look good, you should move them into this or another <table> as well.

dataTables columnFilter no way to enter data for column filtering

The page shows foot with column names but no places to enter the values for column filters.
I looked to the examples on the data tables web site and others in the stack overflow website and found no clue how to fix it.
Any help or suggestion how to fix this will be much appreciated.
Thank you.
<% provide(:title, 'TBMs Daily Progress') %>
<% if signed_in? %>
<h3>TBMs Max Station by day - Today's date: <%=#current_date%></h3>
<% if #current_date != #tbms_progress_W.daily_date %>
<h6 class="error-message">
<%= "Today's location for TBM W is not loaded" %>
<%= " Last TBM W recorded date was: " %>
<%=#tbms_progress_W.daily_date %>
</h6>
<% end %>
<% if #current_date != #tbms_progress_E.daily_date %>
<h6 class="error-message">
<%= "Today's location for TBM E is not loaded" %>
<%= " Last TBM E recorded date was: " %>
<%=#tbms_progress_E.daily_date %>
</h6>
<% end %>
<p>
<% if current_user.admin? %>
<%= link_to "Add new location", new_tbms_progress_path, class: "btn btn-sm btn-danger" %>
<% end %>
<%= link_to "List view", tbms_progresses_path, class: "btn btn-sm btn-primary" %>
</p>
<table id="progresstable" class="display table-bordered table-condensed table-responsive table-hover">
<thead style="background-color: #bbbbbb;">
<tr>
<th>Daily Date</th>
<th>TBM ID</th>
<th>Max Station</th>
<th>TBM Status</th>
<th>ID</th>
</tr>
</thead>
<tbody>
<% #tbms_progresses.each do |tbms_progress| %>
<tr>
<td><%= tbms_progress.daily_date %></td>
<td><%= tbms_progress.tbm_id %></td>
<td><%= number_with_precision tbms_progress.max_station, precision: 2 %>
<td><%= tbms_progress.tbm_status %></td>
<td><%= link_to tbms_progress.id, tbms_progress %></td>
</tr>
<% end %>
</tbody>
<tfoot>
<tr>
<th>Daily Date</th>
<th>TBM ID</th>
<th>Max Station</th>
<th>TBM Status</th>
<th>ID</th>
</tr>
</tfoot>
</table>
<hr/>
<h6>Legend: W aka TBM1 or 26900, E aka TBM2 or 27000</h6>
<% else %>
<%= render 'instruments/unsigned' %>
<% end %>
<script>
$(document).ready
(function()
{
$('#progresstable').dataTable
(
{
/*sPaginationType: "full_numbers",*/
bJQueryUI: true,
aaSorting: [ [0,"desc"], [1, "desc"] ],
bDeferRender: true,
bStateSave: true
}
).columnFilter
(
{aoColumns:
[
{ type: "text" },
{ type: "text" },
{ type: "text" },
{ type: "text" },
{ type: "text" }
]
}
);
}
);
</script>
Sorry, didn't know a plugin like this existed.
I have checked it out and it works perfectly good.
Look for yourself
Only thing i can imagine ist that you did not include the plugincode from here after jquery and datatables code has loaded, but before you start the initialisation.
<script src="http://code.jquery.com/jquery-2.0.3.min.js" data-semver="2.0.3" data-require="jquery#*"></script>
<link data-require="datatables#*" data-semver="1.9.4" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/datatables/1.9.4/css/jquery.dataTables.css" />
<script data-require="datatables#*" data-semver="1.9.4" src="//cdnjs.cloudflare.com/ajax/libs/datatables/1.9.4/jquery.dataTables.js"></script>
<script src="colfilter.js"></script>
<script src="script.js"></script>
I solved this by moving to the data tables 1.10.2, not using anymore bootstrap layout for the tables and I running the scripts for data tables from //cdn.datatables.net on each page containing a table.

Resources