Endless Scrolling gives template error on scrolling down to next page

时间:2015-10-29 11:08:32

标签: jquery ruby-on-rails infinite-scroll will-paginate

I have done pagination using will_paginate gem now I want endless scrolling using this gem only and I have followed railcast and this SO link and written all the codes regarding this. The problem is data comes fine but it is repeating many times on the page when I scroll down to it. I don't know why it is giving this. Any help will be appreciable.

My Controller Code :

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 %>

1 个答案:

答案 0 :(得分:0)

在您正在location.js.erb render @merchants_data进行_merchant.html.erb期待,该广告将为该数据中的每个Merchant实例呈现部分id="locations"。我希望这是rails正在寻找的模板,而不是查找和报告错误。 “渲染集合”下的rails guide on layouts and rendering中有更多详细信息。

所以,你需要提供一个部分来呈现商家或者你的呈现方式略有不同(太多选项)。

你也可能有其他问题。我没有在你的html中看到{{1}}的任何内容,因此很难将任何渲染附加到其中。