我正在尝试实现Railscasts第114集中显示的无限页面功能。分页效果很好,但无穷无尽的页面功能根本不会触发。我没有看到任何错误;只是分页好像我没有添加无尽的页面JavaScript。我的代码:
活动控制器
class ActivitiesController < ApplicationController
before_filter :authenticate_user!
def index
@activities = PublicActivity::Activity.order("created_at DESC").where(owner_type: "User", owner_id: current_user.followed_users.map {|u| u.id}).page(params[:page]).per_page(15)
@post = current_user.posts.build
respond_to do |format|
format.html
format.json
format.js
end
end
end
activities.js.coffee
jQuery ->
if $('.pagination').length
$(window).scroll ->
url = $('.pagination .next_page').attr('href')
if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
$('.pagination').text("Loading more activities...")
$.getScript(url)
$(window).scroll()
index.js.erb的
$('#activities').append('<%= j render ('shared/activities') %>');
<% if @activities.next_page %>
$('.pagination').replaceWith('<%= j will_paginate('shared/activities') %>');
<% else %>
$('.pagination').remove();
<% end %>
<% sleep 1 %>
共享/ _activities.html.erb
<div class="activities">
<% @activities.each do |activity| %>
<code>
<% end %>
<%= will_paginate @activities %>
</div>
问题必须是javascript,但我无法理解。关于可能导致问题的任何想法?
谢谢! -b
答案 0 :(得分:1)
我设法在Rails 4中没有“index.js.erb”文件。只需添加此coffeescript:
$(window).on 'scroll', ->
if $('.pagination').length
@url = $('.pagination .next_page').attr('href')
if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
$('.pagination').remove()
$('#articles').append('<div>')
$('#articles div').last().load @url+' #articles', ->
if $('.next_page.disabled').length
$('.pagination').remove()
这将加载下一页,然后在新加载的页面上使用分页,直到没有其他页面为止。只需在脚本中将“#articles”替换为容器的id即可。跳过index.js.erb和respond_to jazz。
答案 1 :(得分:0)
这里是我的代码中的相关文件,其中我有无尽的页面处理分页。希望您能从下面找出具体的解决方案:
我的index.js.erb文件
$('.carousel').append("<%= j render ('shared/recentlyadded') %>");
<% if @recently_added_video.next_page %>
$('.pagination').replaceWith("<%= j will_paginate(@recently_added_video) %>");
<% else %>
$('.pagination').remove();
<% end %>
我的index.html.erb文件
<div class="carousel"><%= render partial: 'shared/recentlyadded' %></div>
<%= will_paginate @recently_added_video %>
我的_recentlyadded.html.erb文件
<ul class="jcarousel-skin-recent">
<% @recently_added_video.each do |v| %>
<li>
<code>
</li>
<% end %>
</ul>
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery('.jcarousel-skin-recent').jcarousel({
wrap:'both',
animation:'slow'
});
});
</script>
我的javascript文件
jQuery ->
if $('.pagination').length
$(window).scroll ->
url = $('.pagination .next_page').attr('href')
if url && $(window).scrollTop() > $(document).height() - $(window).height() - 225
$('.pagination').text("...")
$.getScript(url)
$(window).scroll()