J'ai une application rails que j'utilise will_paginate
pour. J'utilise ensuite will_paginate
comme base pour le défilement infini. Après beaucoup d'essais et d'erreurs, la demande semble fonctionner, mais au lieu de rendre la page de contenu suivante dans l'application, la requête est en train de ré-afficher tout mon contenu et de l'afficher sous forme paginée. J'ai une pensée que c'est à faire avec mon itérateur .each dans mon partiel, mais je ne suis pas sûr exactement. Ci-dessous se trouve mon contrôleur partiel, js.erb, coffescript et logs. Si quelqu'un peut aider avec une raison pour laquelle cela ne fonctionne pas correctement, je l'apprécierais grandement!Rails de défilement infinis ajax demande de tir, mais pas afficher la page suivante paginée
journaux: vous pouvez voir qu'il devient la page suivante, et il le fait que, pour les 5 pages sur chaque demande:
Started GET "/links?page=2&_=1451404304001" for ::1 at 2015-12-29 10:51:46 -0500
Processing by LinksController#index as JS
Parameters: {"page"=>"2", "_"=>"1451404304001"}
Link Load (0.2ms) SELECT "links".* FROM "links" LIMIT 5 OFFSET 5
(0.1ms) SELECT COUNT(*) FROM "links"
Link Load (0.2ms) SELECT "links".* FROM "links" ORDER BY "links"."cached_votes_score" DESC LIMIT 5 OFFSET 5
CACHE (0.0ms) SELECT COUNT(*) FROM "links"
_link.html.erb:
<!-- order links based on total number of votes -->
<div class="link row clearfix">
<h2>
</h2>
<h2>
<%= link_to link.title, link %><br>
</h2>
<p>
<%= link_to link.url, link %><br>
</p>
<!-- acts_as_votable for like_link -->
<div class="btn-group">
<%= link_to like_link_path(link), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-up"></span>
Upvote
<%= link.get_upvotes.size %>
<% end %>
<%= link_to dislike_link_path(link), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-down">
Downvote
<%= link.get_downvotes.size %>
<% end %>
</div>
</div>
index .js.erb:
$('#links').append('<%= j render(@links) %>');
<% if @links.next_page %>
$('.pagination').replaceWith('<%= j will_paginate @links.next_page %>');
<% else %>
$('.pagination').remove();
<% end %>
link.js.coffee:
jQuery ->
$(window).scroll ->
if $(window).scrollTop() > $(document).height() - $(window).height() - 50
$.getScript($('.pagination a.next_page').attr('href'))
action index de links_controller.rb:
class LinksController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show]
def index
@links = Link.order(cached_votes_score: :desc).paginate(page: params[:page], per_page: 5)
respond_to do |format|
format.js
format.html
end
end
end
index.html.erb:
<div class = "sort_paginate_ajax"><%= render @links %></div>
<div id="quotes_links">
<%= will_paginate @links %>
</div>
Lorsque j'ai changé index.js.erb au changement recommandé dans le premier commentaire par Miles , je reçois maintenant l'erreur de méthode non définie suivante:
ActionView::Template::Error (undefined method `total_pages' for 3:Fixnum):
1: $('#links').append('<%= j render(@links) %>');
2: <% if @links.next_page %>
3: $('.pagination').replaceWith('<%= j will_paginate @links.next_page %>');
4: <% else %>
5: $('.pagination').remove();
6: <% end %>
Essayez de changer '' '$ ('. Pagination '). ReplaceWith (' <% = j will_paginate links%> ');' '' à '' '$ ('. Pagination '). ReplaceWith (' <% = j will_paginate links.next_page%> '); '' 'avec les liens @ sur. SO m'a fait les enlever dans mon commentaire – MilesStanfield
@MilesStanfield J'ai changé la syntaxe replaceWith et je reçois maintenant la nouvelle erreur que je viens de poster ci-dessus, 'méthode non définie 'total pages'' – Jbur43