如果我使用我的自定义 headers 排序,无法弄清楚为什么搜查不起作用。

Can't figure out why ransack wouln't work if I am using my custom headers sorts.

问题出在我的控制器上 我正在使用 ransack 进行搜索,我也有自定义 sortable table headers。我遇到的问题是,当我搜索某些东西时,它会返回所有内容,而不仅仅是搜查搜索值。我对 table headers 工作的自定义排序,但是,无论我搜索什么,它仍然会显示所有内容,而不仅仅是我的搜索结果。任何帮助将不胜感激!!

这是我的入口控制器

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

这是我自定义 table 排序 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

这是我的应用程序控制器,它设置了搜索 ransack..

class ApplicationController < ActionController::Base


  before_filter :set_search

  def set_search
   @search = Entry.ransack(params[:q])
  end
end 

这是我的看法

 = 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
     -@entry.each 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

想通了,我摆脱了我的自定义排序,而是这样做了..

  %th.ts{:style => 'border: solid black;'}= sort_link(@search, :indirect_id, "Indirect ID")

完美运行!!