问题
I've been following the railscast on endless page and had previously done the same for will_paginate, but I'm getting errors however I try to implement it in my specific situation. The will_paginate gems works perfectly, as does the jQuery call to the dom as far as I can tell from reading the developer tools in Chrome. The issue is with what is being returned - rails doesn't like it. Here's some code:
In videos.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 videos...")
$.getScript(url)
$(window).scroll()
In my views/users/index.js.erb
$('.carousel').append(' <%= j render(@recently_added_video) %>');
<% if @recently_added_video .next_page %>
$('.pagination').replaceWith('<%= j will_paginate(@recently_added_video) %>');
<% else %>
$('.pagination').remove();
<% end %>
In my shared partial, which is being rendered by users/index.erb.html
<div class="carousel" %>
<ul class="jcarousel-skin-recent">
<% @recently_added_video.each do |v| %>
<li><%= link_to image_tag(v.video_thumb(), width: 300), v %></li>
<% end %>
</ul>
</div>
<%= will_paginate @recently_added_video %>
This gives me the following error from my development log:
Completed 500 Internal Server Error in 15ms
ActionView::Template::Error (Missing partial videos/video with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}.
So I then tried creating a views/videos/_video.js.erb
partial with the same code as the users/index.js.erb
file, but it then gives me the following error:
Rendered shared/_recentlyadded.html.erb (853.8ms)
Rendered users/index.html.erb within layouts/application (1741.4ms)
Rendered layouts/_header.html.erb (2.6ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 1813ms (Views: 1801.0ms | ActiveRecord: 8.5ms | Sphinx: 0.0ms)
Illegal instruction: 4
and as you can see, it hasn't actually rendered the _video
partial either.
In case it's of relevance, the instance variable @recently_added_video
I'm trying to call is defined in the application_controller
and works fine with the partial and will_paginate
.
Can anyone help figure out what's going wrong? Very confused! Thanks...
回答1:
Nevermind - I figured this out.
The problem was with this line
$('.carousel').append( ' <%= j render(@recently_added_video) %>' );
in the users/index.js.erb file.
I was following the railscast, but Ryan Bates has his instance variable set up to render a partial, so his code works, but because @recently_added_video isn't a partial and won't render one, it was returning the 500 error because there was no partial to find.
What I needed to do was actually render a partial like this:
$('.jcarousel-skin-recent').append(' <%= j render ('shared/recentlyadded') %>');
Hope that helps anyone else who follows the railscast and has issues like I did.
来源:https://stackoverflow.com/questions/13961933/endless-page-with-will-paginate-not-working-with-partial