Apparently I was missing an 'a' in the jQuery code. So this line:
$.getScript($('.pagination .next_page').attr('href'));
Needed to be:
$.getScript($('.pagination .next_page a').attr('href'));
質問
I'm trying to get endless page scrolling to work with my ActivitiesController but all it keeps doing is appending the first page of items. I'm not sure how to get it to load the next pages instead.
ActivitiesController
class ActivitiesController < ApplicationController
def index
params[:page] ||= 1
@activities = Activity.for_member(current_member, params)
end
end
Activities Model
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
Endless Page 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 Partial
<% @activities.each do |activity| %>
<%= render partial: "activities/#{activity.targetable_type.underscore}/#{activity.action}",
locals: { activity: activity } %>
<% end %>
解決
Apparently I was missing an 'a' in the jQuery code. So this line:
$.getScript($('.pagination .next_page').attr('href'));
Needed to be:
$.getScript($('.pagination .next_page a').attr('href'));