Rails3/will_paginate/Ajax - next/previous link doe

2020-07-26 11:47发布

问题:

I am following the "Pagination with ajax" railscast for my rails 3 application. Everything seems to work great, except the fact that Previous and next links don't work at all.

Basically,

  • It appears that the previous link is stuck even if I click on other pages, (when I am on the user/show page1)

  • The "next link" works one time till the page 2 (I can't use it after)

  • Numbers didn't update when I come to "..."

I would be pleased to know if someone experience the same problem (a bug?) or if my code is wrong. I use will paginate ("~> 3.0.pre2"and jquery 1.8.10) for a list of microposts dependent to users.

Thanks a lot for your help!

here the code:

layout_helper.rb

module LayoutHelper
  def javascript(*args)
    content_for(:head) { javascript_include_tag(*args) }
  end
end

users_controller.rb

def show
    @user = User.find(params[:id])
    @microposts = @user.microposts.paginate(:per_page => 10, :page => params[:page])
end

microposts/_micropost.html.erb

...

users/show.html.erb

<% javascript "pagination" %>

...

<%= will_paginate @microposts %>
  <ul id="feed_items">
    <%= render @microposts %>
  </ul>

users/show.js.erb

$("#feed_items").html("<%= escape_javascript(render @microposts ) %>");

pagination.js

$(document).ready(function() {
  $(".pagination a").live("click", function() {
    $.get(this.href, null, null, "script");
    return false;   
  });
});

回答1:

I suggest you look at http://railscasts.com/episodes/254-pagination-with-kaminari .

will_paginate is not updated to work with Rails 3.



回答2:

Or, if you want to stick with will_paginate there's a simple fix. Just put your helper method in a div like so:

<div id="page_links">
 <%= will_paginate @microposts %>
</div>

Then, in your js.erb file add this line to the end:

$('#page_links').html('<%= escape_javascript(will_paginate @microposts) %>');

You're already passing page and all the right params there anyway, so this will just force the view links to update properly.