Search code examples
javascriptjqueryruby-on-railsinfinite-scrollwill-paginate

js.erb working locally but not in production


I followed the tutorial here http://railsforbeginners.com/chapters/chapter-9-infinite-scroll/ for an infinite scrolling. The code works good locally but when I deploy it to prod. the pagination links (1 2 3 4) still show and the infinite scrolling doesn't fire. I also tried to add these files in assets.rb with no success

First of I'm using Rails 4, my application.js looks like this

//= require jquery2
//= require jquery.turbolinks
//= require jquery_ujs
//= require jquery-ui.min
//= require bootstrap-hover-dropdown.min
//= require bootstrap.min
//= require select2
//= require infinite_scroll
//= require turbolinks

Controller action

respond_to do |format|
      format.html
      format.js { render "visitors/index" }
end

index.js.erb

$('#my-articles').append('<%= j render @articles %>');
<% if @articles.next_page %>
$('.pagination').replaceWith('<%= j will_paginate @articles %>');
add_tweets();
<% else %>
$(window).off('scroll');
$('.pagination').remove();
<% end %>



function add_tweets(){
  <% @articles.each do |article|%>
    handle_open_modal("<%= article.id %>");
  <%end%>

}

Solution

  • Here is what I did to get it working using This Reference

    I removed turbolinks and all it's reference

    Then added this to the index.html.erb without have to use jQuery $(document).on('ready

    if ($('#infinite-scrolling').size() > 0){
            $(window).on('scroll', function(){
              var more_posts_url = $('.pagination .next_page a').attr('href')
              heights = $(document).height() - $(window).height() - 500
                if(more_posts_url && $(window).scrollTop() > heights){
                    $('.pagination').html('<img src="/assets/ajax-loader.gif" alt="Loading..." title="Loading..." />')
                    $.getScript(more_posts_url)
                }
            });
        }
    

    Then all the *.js.erb start working.

    I was trying to avoid removing turbolinks but done trying to get it working with it.