Multiple pagination with kaminari via Ajax

mfq picture mfq · Jan 26, 2012 · Viewed 8.2k times · Source

I want to apply multiple pagination with Kaminari via Ajax now here is my code for controller

def user_note
    @user = current_user
    @notes = Bookmark.where('user_id = ? && note is not NULL',current_user.id).order('created_at DESC').page(params[:page_1]).per(4)

    @bookmarks = Bookmark.where('user_id = ? && note is NULL',current_user.id).order('created_at DESC').page(params[:page_2]).per(4)

    respond_to do |format|
      format.html
      format.xml{ render :xml => @user}
    end   end

now for views i have two partials to render this arrays

<div id="bookmarks">
<%= render :partial =>"users/bookmark",:locals => { :bookmark => @bookmarks} %>
            </div>
<%= paginate @bookmarks,:remote => true, :param_name => 'page' %>

inner partial is

<% bookmark.each do |bookmar| %>
  <%= render :partial => 'show_bookmark.html.erb' , :locals => { :bookma => bookmar} %>
<%end%>

script for pagination update is being handled in a separate file

$('#bookmarks').html('<%= escape_javascript render(:partial =>"users/bookmark",:locals => { :bookmark => @bookmarks}) %>');
$('#paginator').html('<%= escape_javascript(paginate(@bookmarks, :remote => true).to_s) %>');

But by doing every thing it is not updating to state of page neither the contain in the page.

Answer

Nadeem Yasin picture Nadeem Yasin · Jan 26, 2012

you are missing to pass params at this line

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

i think it should be like this

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

and you are also passing wrong param at this line

<%= paginate @bookmarks,:remote => true, :param_name => 'page' %>

it should be like this

<%= paginate @bookmarks,:remote => true, :param_name => 'page_2' %>

and please also check that whether you are sending the response correctly to the JS file or not.