【发布时间】:2016-01-29 11:55:35
【问题描述】:
我已经使用 will_paginate gem 完成了分页,现在我只想使用这个 gem 进行无休止的滚动,我已经关注了 railcast 和这个 SO 链接并编写了所有与此相关的代码。问题是数据很好,但是当我向下滚动到它时它在页面上重复了很多次。我不知道为什么它给这个。任何帮助都将不胜感激。
我的控制器代码:
def locations
merchants = Merchant.where("role = ?", 'Merchant')
@merchants_data = merchants.paginate(:page => params[:page], :per_page => 20)
respond_to do |format|
format.html
format.js
end
end
_location.html.erb:
<table>
<tr>
<th>
Name
</th>
</tr>
<% @merchants_data.each do |x| %>
<tr>
<td>
<%= x.name %>
</td>
</tr>
<% end %>
</table>
<%= will_paginate @merchants_data %>
home.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('Fetching more locations...')
$.getScript(url)
$(window).scroll()
location.js.erb:
$('#locations').append('<%= j render partial: "locations", collection:@merchants_data %>');
<% if @merchants_data.next_page %>
$('.pagination').replaceWith('<%= j will_paginate(@merchants_data) %>');
<% else %>
$('.pagination').remove();
<% end %>
【问题讨论】:
-
错误信息是什么?
-
@huanson 给 ActionView::MissingTemplate 错误。
-
对不起,说 exception_class 太愚蠢了!你已经说过它缺少模板。显然我们(或我)询问了它的投掷路径,所以他正在寻找哪个模板却找不到。 ;)
-
我已经渲染了部分并获得了数据,但问题仍然是数据重复了几次。
标签: jquery ruby-on-rails infinite-scroll will-paginate