让我先说明这是一个使用很多其他人的代码并没有完全理解正在发生什么以及如何解决我的问题的实例。rails无限滚动ajax动作页上> 1项
我跟着Railscast无限滚动使用will_paginate,并且工作正常。
我每页加载12个项目,每个项目都有自己的部分内容。每个项目都是用户可以通过单击“赞”按钮来“喜欢”的对象。
我ajax-ified这个按钮,并重新加载部分为'喜欢'的项目。
这对项目1-12(第一页)正常工作。
当用户单击通过无限滚动加载的项目上的竖起大拇指按钮时,ajax调用会执行,但不会再次找到该对象,并且局部变为空白。
这里是我的代码 -
#@messages partial
<div class="span3 well" style="height:360px;" id="message-<%=message.id%>">
<div class="thumbnail">
<%= image_tag message.gif.url %>
<div class="caption">
<h5><%=link_to message.sender, "/profile/#{User.find_by_username(message.sender).id}"%></h5>
<h5><%=message.chain.subject %></h5>
<p><em>"<%= message.content %>"</em></p>
<p><a href="/upvote/<%=message.id%>" data-remote="true" class="btn btn-success"><%=message.up_votes%>
<i class="icon-thumbs-up"></i> Nice Work</a>
</div>
</div>
</div>
# js.coffee (from railscast)
jQuery ->
$(window).scroll ->
url = $('.pagination .next_page').attr('href')
if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
$('.pagination').text('Loading more gifs...')
$.getScript(url)
# vote.js -- js executed by ajax call
$("#message-<%=params[:id]%>").load(location.href+" #message-<%=params[:id]%>>*", "");
我敢肯定的问题是在vote.js. .load(location.href+" #message-<%=params[:id]%>>*", "");
似乎无法找到第2,3,4等页面上的对象,因为它使用了location.href
,它们作为当前url只提供前12个对象。我想我需要以某种方式将网页放在该网址中,但我不确定如何完成此操作。
EDIT- 另外,也许我应该有一个端点,所有未分页的邮件,并调用该URL而不是location.href +
?