Javascript 使用will_paginate更新ajax内容

Javascript 使用will_paginate更新ajax内容,javascript,jquery,ruby-on-rails,ajax,will-paginate,Javascript,Jquery,Ruby On Rails,Ajax,Will Paginate,如何更新视图以保留所有现有的ajax和将分页功能 我有一个页面rehome.html.erb <div id="options">Option Select Here</> <div class="all_animals"> <%= render @animals %> </div> <% unless @animals.current_page == @animals.total_pages %> <di

如何更新视图以保留所有现有的ajax和将分页功能

我有一个页面
rehome.html.erb

<div id="options">Option Select Here</>

<div class="all_animals">
  <%= render @animals %>
</div>

<% unless @animals.current_page == @animals.total_pages %>
  <div id="infinite-scrolling">
    <%= will_paginate @animals %>
  </div>
<% end %>

// WILL_PAGINATE
<script type="text/javascript">
$(function(){

 if($('#infinite-scrolling').size() > 0) {
  $(window).on('scroll', function(){
    //Bail out right away if we're busy loading the next chunk
     if(window.pagination_loading){
        return;
     }

    more_url = $('.pagination a.next_page').attr('href');

  if(more_url && $(window).scrollTop() > $(document).height() - $(window).height() - 50){
    //Make a note that we're busy loading the next chunk.
     window.pagination_loading = true;
    $('.pagination').text('Loading.....');
       $.getScript(more_url).always(function(){
          window.pagination_loading = false;
       });
    }
  });
 }
});
</script>
控制器

def rehomed
# conditions logic
@animals = Animal.joins(:user).where(conditions).paginate(:page => params[:page], :per_page => 6)

 respond_to do |format|
  format.js {render json: @animals }
 end
end
我想做的是加载新集合(再次分页为每页6个),当我向下滚动时,只显示属于@animals新集合的对象(如果有)

当我向下滚动页面加载原始集合时,分页链接不会更新

编辑 因此,我创建了一个
rehome.js.erb
文件,它与我的
rehome.js.erb
几乎相同:

$('.all_animals').empty();
$('.all_animals').append('<%= j render @animals %>');
 <% if @animals.next_page %>
   $('.pagination').replaceWith('<%= j will_paginate @animals %>');
 <% else %>
  $(window).off('scroll');
  $('.pagination').remove();
 <% end %>
因此加载新的动物集合,重新创建分页链接,但使用重新安置的url,例如:

之前 编辑2 我已经实现了@japed的答案,但是现在在呈现新集合之后,分页将继续呈现整个db集合,包括重复为新集合选择的那些,它正在重复自己


如何确保为我的链接生成正确的url?

将分页允许您设置参数哈希,以便您可以将其更改为使用其他控制器操作,如下所示:

will_paginate(@animals, :params => { :controller => "public", :action => "rehomed" })

我对你的问题有点困惑。您只需要在select或onchange事件上单击一次binder@japed抱歉,我已经更新了我的问题,希望能进一步解释。谢谢,这确实解决了我设置正确链接的问题,这很好,但出于某种原因,现在,一旦我到达新结果集合的末尾,它就会继续进行,然后加载整个数据库中留下的所有记录
def rehomed
# conditions logic
@animals = Animal.joins(:user).where(conditions).paginate(:page => params[:page], :per_page => 6)

 respond_to do |format|
  format.js {render json: @animals }
 end
end
$('.all_animals').empty();
$('.all_animals').append('<%= j render @animals %>');
 <% if @animals.next_page %>
   $('.pagination').replaceWith('<%= j will_paginate @animals %>');
 <% else %>
  $(window).off('scroll');
  $('.pagination').remove();
 <% end %>
respond_to do |format|
  format.js 
end
<a class="next_page" href="/public/rehome?page=2" rel="next">Next →</a>
<a class="next_page" href="/public/rehomed?page=2" rel="next">Next →</a>    
$('.pagination').text('Loading.....');
will_paginate(@animals, :params => { :controller => "public", :action => "rehomed" })