我试图让无休止的页面滚动与我的活动控制器一起工作,但它所做的只是附加项目的第一页。我不知道如何让它加载下一页。
活动控制器
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 %>