2

I have successfully set up an ajax pagination with Kaminari.

In order to add a sort feature I followed this great Railscast and replaced will_paginate by Kaminari.

The pagination works great but the sort works just the first time because the sort_column and the sort _direction are not updated.

I can't figure out why.

Here is my code :

Controller :

def index
 @questions = Question.order(sort_column + " " +  sort_direction).page(params[:page])
end

....

private

def sort_column
 Question.column_names.include?(params[:sort]) ? params[:sort] :  "created_at"
end

def sort_direction
 %w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
end

I have added :remote => true, :method => :get to

ApplicationHelper :

def sortable(title = nil, column)
 title ||= column.titleize
 direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
 link_to title, params.merge(:sort => column, :direction => direction), :remote => true, :method => :get
end

index.html.erb :

<%= sortable "created_at" %><br/>
<div id="questions"><%= render @questions %></div>
<div id="paginator"><%= paginate @questions, :remote => true %>

index.js.erb :

$('#questions').html('<%= escape_javascript(render @questions) %>');
$('#paginator').html('<%= escape_javascript(paginate(@questions, :remote => true).to_s) %>');

Is this a problem with the :remote => true, :method => :get part ? If I remove it the sort works but without ajax.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
invaino
  • 267
  • 1
  • 4
  • 13

1 Answers1

4
@products = Product.order("name").page(params[:page])  

http://asciicasts.com/episodes/254-pagination-with-kaminari

aadarshsg
  • 2,069
  • 16
  • 25