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

时间:2015-04-23 12:36:35

标签: ruby-on-rails ruby ruby-on-rails-4 ransack

问题在我的控制器中我使用ransack进行搜索,而且我还有自定义的可排序表头。我遇到的问题是当我在搜索某些内容时它会返回所有内容,而不仅仅是搜索搜索值。我的桌面标题上的自定义排序工作,但是,无论我搜索它仍然显示我的一切,而不仅仅是我的搜索结果。任何帮助将不胜感激!!

这是我的入口控制器

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

这是我的自定义表格排序标题的辅助方法

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

这是我的应用程序控制器,它具有搜索搜索范围..

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

1 个答案:

答案 0 :(得分:0)

想出来我摆脱了我的自定义排序而只是做了这个......

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

完美的作品!!