Search code examples
ruby-on-railswill-paginatemeta-search

Rails 3 meta_search sort_link and pagination


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 :(


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.