ruby on rails - Search results will not sort by column headers? -
i have search feature on site. results page give paginated results whatever user sets to, not sort results column headers.
i can click on them , desc/asc arrows, page doesn't sort results accordingly.
here results page:
.row-fluid#load_results_table .span12 - if results.empty? =render partial: 'no_results', locals: {search: search} - else %h1.page-header search results - if results.count > 1000 %p= "your search yielded many #{search.search_type.pluralize}" - else %p= "your search yielded #{results.count} #{search.search_type.pluralize}" =render partial: 'header_buttons', locals: {search: search} %br %br = will_paginate results, :renderer => bootstrappagination::rails, id: "first_pagination" %table.table.search_results.table-condensed.table-bordered.table-striped.sortable{:id => "#{search.search_type.pluralize}"} %thead %tr.search_header %th= sortable_load "origin" %th= sortable_load "dest" %th= sortable_load "pickup" %th= sortable_load "delivery" %th= sortable_load "ltl" %th= sortable_load "equipment_id", "equipment" %th= sortable_load "weight" %th= sortable_load "length" %th= sortable_load "rate" -unless @search.origin.blank? %th estimated deadhead miles %th actions %tbody - results.each |result| %tr{:class => "#{search.search_type}_view", :id => "#{result['id']}" } %td= location.to_cs(result.origin) %td= location.to_cs(result.dest) %td= format_date(result.pickup) %td= format_date(result.delivery) %td= full_or_par(result.ltl) %td= result.equipment_id ? equipment.to_equipment_name(result.equipment_id) : "" %td= result.weight %td= result.length %td= result.rate -unless @search.origin.blank? %td= location.distance_between(@search.origin.coords, result['origin']) %td .btn-group %a.btn.btn-info{ :href => "/#{search.search_type.pluralize}/#{result['id']}" } show %a.btn{ :href => "javascript:void(0)", :class => "save", user_id: @search.user_id, :id => result['id']} save %a.btn.btn-primary{ :href => "javascript:void(0)", :class => "cover_link", :user_id=> @search.user_id } cover = will_paginate results, :renderer => bootstrappagination::rails
search model
class search < activerecord::base attr_accessible :available, :delivery, :dest, :length, :ltl, :origin, :pickup, :rate, :search_type, :user_id, :weight, :expiration, :pickup_operator, :delivery_operator, :expiration_operator, :available_operator, :length_operator, :rate_operator, :weight_operator, :origin_zip, :dest_zip, :origin_radius, :dest_radius, :saved, :date_posted, :order_by, :origin_cs, :results, :dest_cs, :origin_states, :dest_states, :origin_id, :dest_id, :equipment_ids, :temp_origin, :temp_dest, :temp_equipment attr_accessor :origin_cs, :dest_cs, :log before_validation :convert_cs_to_id operators = ["<=","<",">=",">","=","!="] numericality_message = "needs number" location_format_message = "location must in format (city,st)" location_presence_message = "location not exist" validates_inclusion_of :pickup_operator, in: operators validates_inclusion_of :delivery_operator, in: operators validates_inclusion_of :expiration_operator, in: operators validates_inclusion_of :available_operator, in: operators validates_inclusion_of :length_operator, in: operators validates_inclusion_of :rate_operator, in: operators validates_inclusion_of :weight_operator, in: operators validates_inclusion_of :search_type, in: ["load","truck"] validates_numericality_of :rate, {allow_nil: true, message: numericality_message} validates_numericality_of :origin_radius,{allow_nil: true, message: numericality_message} validates_numericality_of :dest_radius, {allow_nil: true, message: numericality_message} validates_numericality_of :weight, allow_nil: true, message: numericality_message validates_numericality_of :length, allow_nil: true, message: numericality_message validates_presence_of :search_type validates_presence_of :origin_id, :if => :origin_cs? validates_presence_of :dest_id, :if => :dest_cs? belongs_to :user has_and_belongs_to_many :equipment belongs_to :origin, class_name: "location", foreign_key: :origin_id belongs_to :dest, class_name: "location", foreign_key: :dest_id def search(page) = [] << preparesearch.states("dest", self.dest_states) unless self.dest_states.blank? << preparesearch.states("origin", self.origin_states) unless self.origin_states.blank? << preparesearch.location('origin', self.origin.coords, self.origin_radius) unless self.origin.blank? << preparesearch.location('origin', self.origin.coords, self.origin_radius) unless self.origin.blank? << preparesearch.zip('origin', self.origin_zip, self.origin_radius) unless self.origin || self.origin_zip.blank? << preparesearch.location('dest', self.dest.coords, self.dest_radius) unless self.dest.blank? << preparesearch.zip('dest', self.dest_zip, self.dest_radius) unless self.dest || self.dest_zip.blank? << preparesearch.equipment(self.equipment_ids) unless self.equipment_ids.blank? << preparesearch.date('created_at', self.date_posted, '>=') unless self.date_posted.blank? if self.search_type == 'load' select = "loads.id, origin, dest, pickup, delivery, ltl, equipment_id, weight, length, rate" << preparesearch.date('pickup', self.pickup, self.pickup_operator) unless self.pickup.blank? << preparesearch.date('delivery', self.delivery, self.delivery_operator) unless self.delivery.blank? << preparesearch.attribute('length', self.length, self.length_operator) unless self.length.blank? << preparesearch.attribute('rate', self.rate, self.rate_operator) unless self.rate.blank? << preparesearch.attribute('weight', self.weight, self.weight_operator) unless self.weight.blank? << preparesearch.attribute('ltl', self.ltl, '=') unless self.ltl.blank? elsif self.search_type == 'truck' select = "trucks.id, origin, dest, available, expiration, equipment_id, comments" << preparesearch.date('available',self.available,self.available_operator) unless self.available.blank? << preparesearch.date('expiration',self.expiration,self.expiration_operator) unless self.expiration.blank? end << "covered=false , deleted=false" = where.join(' , ') order = self.order_by ? self.order_by + " desc" : "" limit = "limit=200" module.const_get(self.search_type.capitalize).where(where).select(select).limit(limit).order(order).page(page).per_page(25) end module preparesearch def preparesearch.equipment(equipments) eq = "" equipments.each {|e| eq += "'#{e}'," } eq = eq[0..-2] "equipment_id in (#{eq})" end def preparesearch.attribute(attribute, value, comparison) "#{attribute} #{comparison} #{value}" end def preparesearch.date(type, date, comparison) if comparison == '=' "cast(#{type} text) '%#{date.strftime("%f")}%'" elsif comparison == '<=' "#{type} #{comparison} '#{date.strftime("%f")} 23:59:59'" else "#{type} #{comparison} '#{date.strftime("%f")}'" end end def preparesearch.zip(type, zip, radius) location = location.where(zip: zip).first if location "(st_distance(#{type}, st_geographyfromtext('point( #{location.coords.x} #{location.coords.y} )')) <= #{radius.to_f*1609.334})" else #if no locations match zip search location never exist. "(st_distance(#{type}, st_geographyfromtext('point( 1 1 )')) <= #{radius.to_f*1609.334})" end end def preparesearch.location(type, location, radius) "(st_distance(#{type}, st_geographyfromtext('point( #{location.x} #{location.y} )')) <= #{radius.to_f*1609.334})" end def preparesearch.states(type, states) states = states.split(",") st = "" states.each {|s| st += "'#{s}'," } st = st[0..-2] "#{type}_state in (#{st})" end end @log = logger.new("#{rails.root}/log/searches.log") @log.datetime_format = "%f %t" private def convert_cs_to_id temp_origin = location.where(cs: self.origin_cs.downcase) unless self.origin_cs.blank? self.origin_id = temp_origin.blank? ? "" : temp_origin.first.id temp_dest = location.where(cs: self.dest_cs.downcase) unless self.dest_cs.blank? self.dest_id = temp_dest.blank? ? "" : temp_dest.first.id end def origin_cs? errors.add(:origin_cs, "not valid location") if !origin_cs.blank? && origin_id.blank? origin_cs.blank? ? false : true end def dest_cs? errors.add(:dest_cs, "not valid location") if !dest_cs.blank? && dest_id.blank? dest_cs.blank? ? false : true end end
update
when click on column headers page url changes
http://www.sitename.com/searches/23616?direction=desc&sort=pickup
Comments
Post a Comment