1

I am using the will paginate gem for pagination. Things wok fine for me.I am trying to ajaxify it and I followed the http://railscasts.com/episodes/240-search-sort-paginate-with-ajax tutorial. But Ajax request is not being fired for me.

I have a index view. The view renders a partial called "_browser_form" which in turn renders a partial called "_listing". So I wanted to paginate the table in the _listing. Please let me know if there is any error in my approach.

My controller:

def index

         @ics = Ic.search(params[:root_name],params[:suite_name],params[:case_name],params[:name],'f').paginate(:per_page =>5, :page => params[:all_ics])
         respond_to do |format|
          format.html # index.html.erb
          format.xml  { render :xml => @ics }
        end
      end

My _browser_form.html.haml which is rendered from index.html.haml

- form_tag "/ics/mass_action", :method => :post, :multipart => true do
  <div id="update_ics_table">
  = render "listing", :show_check_boxes => show_check_boxes, :root_name=>params[:root_name],:suite_name=>params[:suite_name],:case_name=>params[:case_name],:name=>params[:name],:ic_filter=>1
  </div>
   =will_paginate @ics,:param_name=>:all_ics

My index.js.erb file:

$('#update_ics_table').html("<%= escape_javascript(render :partial => 'listing' ,:object => @ics) %>")

My .js file:

$(function () {
  $('#update_ics_table .pagination a').live('click',
    function () {
      $.getScript(this.href);
      return false;
    }
  );
});

Thanks, Ramya.

ramya
  • 928
  • 5
  • 14
  • 30

2 Answers2

1

Remove what you're doing in the Javascript and do this in the controller:

format.js {
  render :update do |page|
    page.replace 'listing', :partial => 'listing'
  end
}

Similar: Best way to get will_paginate working with Ajax

Community
  • 1
  • 1
Syed Aslam
  • 8,707
  • 5
  • 40
  • 54
  • Hi Syed , I am seeing this error in the rails log [2012-01-16 14:25:35] ERROR bad URI `/%22/javascripts/pages/ic.js?1326703901\"'. and in the IE GUI the complete thing is enclosed in execption and printed. try { Element.replace("listing", "\n \n \n All:\n Expand\n /\n Collapse\n \n \n Check\n /\n Uncheck\n \n – ramya Jan 16 '12 at 10:23
0

It might have something to do with the fact that you're using HAML views and an js.erb file, but I don't use HAML so I can't be certain. Have a look at nex3's answer at this SO: How to send back js.haml in rails

If that is not the case, I think you need to change your respond_to controller action block to this:

respond_to do |format|
          format.html # index.html.erb
          format.xml  { render :xml => @ics }
          format.js #<------ This will execute index.js.erb
end
Community
  • 1
  • 1
Noz
  • 6,216
  • 3
  • 47
  • 82