方法:对搜索结果进行排序

controller code

  def index
    @jobs = case params[:search] && params[:order]
         when params[:search] && "by_lower_bound"
           Job.published.search(params[:search]).order("wage_lower_bound DESC").paginate(:page => params[:page], :per_page => 5)
         when params[:search] && "by_upper_bound"
           Job.published.search(params[:search]).order("wage_upper_bound DESC").paginate(:page => params[:page], :per_page => 5)
         when  params[:search] && "created_at"
           Job.published.search(params[:search]).order("created_at DESC").paginate(:page => params[:page], :per_page => 5)
         else
           Job.published.search(params[:search]).recent.paginate(:page => params[:page], :per_page => 5)
         end
  end

view code

<div class="dropdown clearfix pull-right">
  <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenuDivider" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
 排序
 <span class="caret"></span>
  </button>

  <ul class="dropdown-menu" aria-labelledby="dropdownMenuDivider">
     <li><%= link_to("按照薪资下限排序", jobs_path(:order => "by_lower_bound", :search => params[:search]))%></li>   
     <li><%= link_to("按照薪资上限排序", jobs_path(:order => "by_upper_bound",:search => params[:search]))%></li>
     <li><%= link_to("按照发表时间排序", jobs_path(:order => "created_at",:search => params[:search]))%></li>
  </ul>
</div>

只要通过前端传送参数即可,controller的case判断多加一个参数判断即可。 有一点需要注意when params[:search] && "by_lower_bound"的两个条件不能颠倒,when "by_upper_bound" && params[:search]这样是不会work的。猜测对条件进行过滤的时候是有先后顺序的。