will_paginate与无限的页面和活动仅附加第一页

时间:2013-10-15 19:52:21

标签: jquery ruby-on-rails-3 android-activity scroll will-paginate

我正在努力让无限的页面滚动与我的ActivitiesController一起工作,但它一直在做的是追加第一页的项目。我不知道如何让它加载下一页。

ActivitiesController

class ActivitiesController < ApplicationController

  def index
    params[:page] ||= 1
    @activities = Activity.for_member(current_member, params)
  end

end

活动模型

class Activity < ActiveRecord::Base
  belongs_to :member
  belongs_to :targetable, polymorphic: true

  self.per_page = 4

  def self.for_member(member, options={})
    options[:page] ||= 1
    following_ids = member.following_members.map(&:id).push(member.id)
    where("member_id in (?)", following_ids).
        order("created_at desc").
        page(options[:page])
  end 
end

无尽的页面jQuery

$(window).scroll(function(){
        if ($(window).scrollTop() > $(document).height() - $(window).height() - 100){
            $('.pagination').text("Loading more...");
            $.getScript($('.pagination .next_page').attr('href'));
        }
    });

index.js.erb的

$('#act_index').append('<%= j render ("activities/activities") %>');
$('.pagination').replaceWith('<%= j will_paginate(@activities) %>');

活动部分

<% @activities.each do |activity| %>

    <%= render partial: "activities/#{activity.targetable_type.underscore}/#{activity.action}",
    locals: { activity: activity } %>

<% end %> 

1 个答案:

答案 0 :(得分:1)

显然我在jQuery代码中缺少'a'。所以这一行:

$.getScript($('.pagination .next_page').attr('href'));

需要:

$.getScript($('.pagination .next_page a').attr('href'));