Reputation: 53
Question is in my controller I am using ransack to search, and I also have custom sortable table headers. The issue I am having is when I am searching for something it's returning everything and not just the ransack search value. my custom sorts on my table headers work but, still no matter what I search for it still shows me everything and not just my search result's. Any help would be greatly appreciated!!
Here is my entry controller
class EntryController < ApplicationController
before_action :authenticate_user!
helper_method :sort_column, :sort_direction
layout 'angular'
def index
#show request for managers based off section...
if current_user.authority == 'M'
if params[:q].nil?
@entry = Entry.where("section = ?", current_user.section).order(sort_column + " " + sort_direction).page(params[:page])
@entry = Kaminari.paginate_array(@entry).page(params[:page])
entry_by_start_date = Entry.where("section = ?", current_user.section).group_by {|i| i.leave_start.to_date}
entry_by_end_date = Entry.where("section = ?", current_user.section).group_by {|i| i.leave_end.to_date}
@entry_by_date = entry_by_start_date.merge(entry_by_end_date) {|key, oldval, newval| newval + oldval}
@date = params[:date] ? Date.parse(params[:date]) : Date.today
else
@q = Entry.ransack(params[:q])
@entry = @q.result.where("section = ?", current_user.section)
@entry = Entry.where("section = ?", current_user.section)
@entry = Kaminari.paginate_array(@entry).page(params[:page])
entry_by_start_date = Entry.where("section = ?", current_user.section).group_by {|i| i.leave_start.to_date}
entry_by_end_date = Entry.where("section = ?", current_user.section).group_by {|i| i.leave_end.to_date}
@entry_by_date = entry_by_start_date.merge(entry_by_end_date) {|key, oldval, newval| newval + oldval}
@date = params[:date] ? Date.parse(params[:date]) : Date.today
end
end
respond_to do |format|
format.html # index.html.haml
private
def sort_column
%w[ indirect_id am_pm emp_first_name leave_start leave_end approve_disapprove ].include?(params[:sort]) ? params[:sort] : 'created_at'
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : 'desc'
end
end
Here is my helper method for the my custom table sort headers
module ApplicationHelper
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "icon-caret" + (sort_direction == 'asc' ? "-up" : '-down') + " icon-large": ""
direction = column == sort_column && sort_direction == 'asc' ? 'desc' : 'asc'
link_to (title + ' <i class="' + css_class + '"></i>').html_safe, :sort => column, :direction => direction, :search => params[:search], :$
end
end
Here is my application controller that has the set search for ransack..
class ApplicationController < ActionController::Base
before_filter :set_search
def set_search
@search = Entry.ransack(params[:q])
end
end
This is my view
= search_form_for @search, :url => entry_index_path, :html => {:method => :get} do |f|
= f.label "Search by Employee Name, Type of Request, or Department."
- 1.times do
%br
= f.text_field :emp_id_or_emp_first_name_or_emp_last_name_or_indirect_id_or_emp_dept_cont, :id => 'search_up', :placeholder => "Search"
= f.submit "Search", :class => "btn btn-primary"
= link_to "Clear Search", entry_index_path, :class => "btn btn-success"
- 3.times do
%br
%table.table.table-bordered.trace-table
%thead
%tr.trace-table
%th.ts{:style => 'border: solid black;'}= sortable ("indirect_id"), "Type Of Request"
%th.ts{:style => 'border: solid black;'}= sortable ("emp_first_name"), "Employee Name"
%th.ts{:style => 'border: solid black;'}= sortable ("emp_dept"), "Department"
%th.ts{:style => 'border: solid black;'}= sortable ("leave_start")
%th.ts{:style => 'border: solid black;'}= sortable ("leave_end")
%th.ts{:style => 'border: solid black;'} Dates Requested
%th.ts{:style => 'border: solid black;'} Requested Amount
%th.ts{:style => 'border: solid black;'}= sortable ("am_pm"), "Shift"
%th.ts{:style => 'border: solid black;'} Employee Comment
%th.ts{:style => 'border: solid black;'}= sortable ("approve_disapprove"), "Approval Status"
%th.ts{:style => 'border: solid black;'} Comment
%th.ts{:style => 'border: solid black;'} Cancel Request
%tr.trace-table
[email protected] do |e|
%tr.trace-table
%td.trace-table{:style => 'width:6%;' 'border: solid black;'}= e.indirect_id
%td.trace-table{:style => 'width:8%;' 'border: solid black;'}
%span.u= e.emp_first_name.capitalize
%span.u= e.emp_last_name.capitalize
%span.u "#{e.emp_id}"
%td.trace-table{:style => 'width:6%;' 'border: solid black;'}= e.emp_dept
%td.trace-table{:style => 'width:6%;' 'border: solid black;'}= e.leave_start.strftime('%m/%d/%y')
%td.trace-table{:style => 'width:6%;' 'border: solid black;'}= e.leave_end.strftime('%m/%d/%y')
%td.trace-table{:style => 'width:6%;' 'border: solid black;'}= e.full_range
%td.trace-table{:style => 'width:6%;' 'border: solid black;'}= e.range_days
%td.trace-table{:style => 'width:6%;' 'border: solid black;'}= e.am_pm
%td.trace-table{:style => 'width:10%;' 'border: solid black;'}= e.sick_day
%td.trace-table{:style => 'border: solid black;'}
%span.u= best_in_place e, :approve_disapprove, :as => :select, :collection => ['Approve', 'Disapprove', 'Pending']
%td.trace-table{:style => 'border: solid black;'}
%span.u= best_in_place e, :tl_mgr_comment, :as => :input
%td.trace-table{:style => 'width:5%;' 'border: solid black;'}= link_to '_', e, :class => "btn btn-danger", method: :delete, data: { confirm: 'Are you sure you want to cancel this time off request?' }
%p=paginate @entry, :window => 2, :outer_window => 2
Upvotes: 1
Views: 180
Reputation: 53
Figured it out I got rid of my custom sort and just did this instead..
%th.ts{:style => 'border: solid black;'}= sort_link(@search, :indirect_id, "Indirect ID")
Works perfectly!!
Upvotes: 0