js.erb 在本地工作但不在生产中

js.erb working locally but not in production

我按照这里的教程 http://railsforbeginners.com/chapters/chapter-9-infinite-scroll/ 进行了无限滚动。该代码在本地运行良好,但当我将其部署到产品时。分页链接 (1 2 3 4) 仍然显示并且无限滚动不会触发。我还尝试在 assets.rb 中添加这些文件,但没有成功

我首先使用 Rails 4,我的 application.js 看起来像这样

//= 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%>

}

在你的assets.rb

试试这一行:

config.assets.precompile += ['Index.js'] 

-> Rails 只能通过其(非 ERB)资产文件名引用文件

另外,作为一种风格,您应该将文件名小写为:index.js.erb

您确定 jQuery 正在加载吗?按照上面的建议,转到开发人员工具网络并重新加载。 您的资产是否在生产环境中预编译? 确保你的 config/application.rb 中有这个 config.serve_static_assets = true 对于 Heroku 和 Rails 4,请参阅 here

这是我为了让它正常工作所做的 This Reference

我删除了 turbolinks 及其所有参考

然后将其添加到 index.html.erb 而不必使用 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)
            }
        });
    }

然后所有 *.js.erb 开始工作。

我试图避免删除 turbolinks,但尝试让它与它一起工作。