简体   繁体   English

Will_paginate,呈现下一页

[英]Will_paginate, render next page

I'am using will_paginate gem to paginate my posts. 我正在使用will_paginate gem来分页帖子。 And I want to do infinite scroll. 而且我想做无限滚动。 I watch few video and read couple of posts, but thats not really what I need (because of my app structure). 我很少看视频并阅读了几篇文章,但这并不是我真正需要的(因为我的应用程序结构)。 You see, home.html.erb (to be short), looks like: 您会看到home.html.erb (简而言之)如下所示:

<div class='news-lent'>
  <%= render 'posts/posts_for_all', posts_variable: @posts %>
</div>

And my _posts_for_all.html.erb (to be short), look like (to be short): 我的_posts_for_all.html.erb (简称)看起来像(简称):

<% posts_variable.each do |post| %>
  <%= link_to post.theme, post %>
<% end %>
<% will_paginate posts_variable, :next_label => "More" %> # => i need it every
time the page is loaded

I've already wrote JS for all, except one line: 我已经为所有代码编写了JS,只有一行:

$('.news-lent').append('<%= j render *some_thing* %>')

Well, I don't know what pass to render. 好吧,我不知道要渲染什么。 I cannot pass my @posts (it will return error(missing partial posts/_post)), and @posts.next_page (it will return 2), and #{posts_path(@post)}?page=#{@post.current_page+1}(but this don't work too) and other problems. 我无法传递我的@posts (它将返回错误(缺少部分帖子/ _post))和@ posts.next_page (将返回2)和#{posts_path(@post)}?page=#{@post.current_page +1}(但这也不起作用)和其他问题。

Can you help me, I need something to put inside j render so it rendered next page? 您能帮我吗,我需要在j渲染中添加一些内容以便渲染下一页?

UPDATE 更新

I reworked everything with RailCast videos, and same way like @Szilard Magyar tell me to do, but now it keep rendering the same (first page) all time and it not rendering second page at all, what can create such problem? 我用RailCast视频重做了所有内容,就像@Szilard Magyar告诉我一样,但是现在它始终呈现相同的内容(第一页),而根本不呈现第二页,怎么会出现这样的问题?

UPDATE 更新

Follow railcast video (reworked my app structure), and a new problem appears: 跟随铁路广播视频(重新设计我的应用程序结构),出现一个新问题:

My home.js.coffee: 我的home.js.coffee:

jQuery ->
  url = $('.pagination .next_page a').attr('href')
  $(window).scroll ->
    if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
      $('.pagination').text("Fetching...")
      $.getScript(url)

My index.js.erb: 我的index.js.erb:

$('.one').append('<%= j render @posts, posts_variable: @posts %>');
  <% if @posts.next_page %>
    $('.pagination').replaceWith('<%= j will_paginate(@posts) %>');
  <% else %>
    $('.pagination').remove();
  <% end %>
<% sleep 3 %>

My index.html.erb: 我的index.html.erb:

<div class="news-lent">
  <div class="one">
    <%= render @posts, posts_variable: @posts %>
  </div>

  <div id="infinite-product-scrolling">
    <%= will_paginate @posts, :next_label => " Еще" %>
  </div>
</div>

But now, when I'am on first page and scroll down it rendering second page, but 2x times, and when I again scrolling to bottom and it rendering second page (it not rendering third page). 但是现在,当我在第一页上并向下滚动时,它呈现第二页,但是是2倍,并且当我再次滚动到底部并在其上呈现第二页时(它不在呈现第三页)。 I don't know how to fix it, can you land me again? 我不知道如何解决,您能再降落我吗?

index.html.erb index.html.erb

<div class="product-index-list new-product-insert">
  <%= render @products %>
</div>
<div id="infinite-product-scrolling">
  <%= will_paginate @products %>
</div>
<div class="no-more">
  <p>No more products.</p>
</div>

_product.html.erb _product.html.erb

<div class="name"><%= product.name %></div>
<div class="oneliner"><%= product.oneliner %></div>

products.js products.js

if ($('#infinite-product-scrolling').size() > 0) {
  $(window).on('scroll', function() {
    $('#infinite-product-scrolling').hide();
    var more_products_url;
    more_products_url = $('#infinite-product-scrolling .pagination .next_page a').attr('href');
    if (more_products_url && $(window).scrollTop() > $(document).height() - $(window).height() - 60) {
      $('.pagination').html("<i class='fa fa-circle-o-notch fa-2x fa-spin'></i>");
      $('#infinite-product-scrolling').show();
      $.getScript(more_products_url);
    }
  });
};

index.js.erb index.js.erb

$('.new-product-insert').append('<%= j render @products %>');
<% if @products.next_page %>
  $('.pagination').replaceWith('<%= j will_paginate @products %>');
<% else %>
  $(window).off('scroll');
  $('.pagination').remove();
  $('.no-more').delay(1000).fadeIn(1500);
<% end %>

Solve it, the problem appears because of my mistake. 解决它,问题是由于我的错误而出现的。 In here: 在这里:

jQuery ->
  url = $('.pagination .next_page a').attr('href')
  $(window).scroll ->

url variable was assigned only one time, before scrolling and that was my mistake. 在滚动之前,只给url变量分配了一次,这是我的错误。 So if url was save, so it always rendered second page and never third. 因此,如果保存了url,则它将始终呈现第二页,而永远不会呈现第三页。 We have to move our url lower, under .scroll event and it will always check, what page have been rendered. .scroll事件下,我们必须将url移低一些,它将始终检查呈现了什么页面。 Like this: 像这样:

jQuery ->
  $(window).scroll ->
  url = $('.pagination .next_page a').attr('href')

By the way, I will recommend to all (if they are using jQuery from RailsCast tutorial ) to add some script to prevent multiple page load when user scroll 顺便说一句,我将建议所有人(如果他们使用的是RailsCast教程中的 jQuery)添加一些脚本以防止用户滚动时加载多页

jQuery ->
  $(window).scroll ->
    url = $('.pagination .next_page a').attr('href')
    return if(window.pagination_loading) # -> add this
    if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
      window.pagination_loading = true # -> and this 
      $('.pagination').text('Fetching products...')
      $.getScript(url).always ->
        window.pagination_loading = false # -> and this

And for prevent bugs etc. to url variable you need to add a , so it look like url = $('.pagination .next_page a').attr('href') and not like in RailsCast tutorial : url = $('.pagination .next_page').attr('href') 并且为了防止错误url = $('.pagination .next_page a').attr('href') url变量中,您需要添加a ,所以它看起来像url = $('.pagination .next_page a').attr('href') ,而不像RailsCast教程中那样url = $('.pagination .next_page').attr('href')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM