Question

Wondering if anyone knew how to get meta_search's sort_link to work with will_paginate?

will_paginate links keep meta_search's search/sort params, but sort_link does not :(

Was it helpful?

Solution

This is the way I did it. I have a Task model with name and deadline attributes.

index.html.haml

= search_form_for @q do |f|
  = f.text_field :name_cont
  = f.submit t(:search)
%table.table
  %thead
    %tr
      %th= sort_link @q, :name, t(:task), {page: params[:page]}
      %th= sort_link @q, :deadline, t(:deadline), {page: params[:page]}
      %th= sort_link @q, :created_at, t(:created_at), {page: params[:page]}
      %th= t(:action)
  %tbody
    - @tasks.each do |task|
        %td= task.name
        %td= task.deadline
        %td= task.created_at
        %td
          = link_to t(:edit), edit_task_path(task)
          = link_to t(:delete), task, :confirm => t(:delete_question), :method => :delete
= will_paginate @tasks

As you can see I send the page param when click in the 'sort_link'.

*tasks_controller.rb*

  def index
    @q = Task.search(params[:q])
    @tasks = @q.result.paginate(:page => params[:page], :per_page => 10)
  end

I hope it helps.

PS: I'm using Ransack instead of MetaSearch.

OTHER TIPS

This pull request might be what you are looking for, unfortunately it is not merged yet.

If you are willing to use a separate gem for sorting links, the sorted gem is built to offer exactly the functionality you are looking for-- sorting columns while saving state across pagination.

https://github.com/mynameisrufus/sorted

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top