Coffeescript / Javascript在服务器上不起作用

我已经实现了“无限滚动”function,如果有更多post要显示,用户可以使用该function继续向下滚动。 我遵循Railscasts,它在本地工作得很好(javascripts和will-paginate gem)。 但是,在服务器上,此function不起作用。 我所看到的只是简单的分页,并且不会应用无限滚动。 我认为这与编译或预处理有关,因为javascript在本地工作正常。

我已经尝试运行bundle exec rake assets:precompile在本地bundle exec rake assets:precompile并部署它。 此外,我尝试在服务器上运行相同的命令。 问题尚未解决。

有没有人对这个问题有一个很好的解释? 相关文件如下:

  1. 应用程序/资产/ JavaScript的/ posts.js.coffee
  2. 应用/视图/ index.js.erb的

假设js文件中的内容很好,因为该function在本地服务器上运行很大。 我几乎可以肯定问题的根源是编译。

更新:

来自Rails指南关于资产管道http://guides.rubyonrails.org/asset_pipeline.html

 When these files(coffescripts) are requested, they are processed by the processors provided by the coffee-script and sass gems and then sent back to the browser as JavaScript and CSS respectively. 

这解释了config / application.rb中的行

 Bundler.require *Rails.groups(:assets => %w(development test)) only loads gems from the assets group in your development and test environment. This means that things like sass-rails and uglifier won't be available in production, which then means that you won't be able to properly compile/minify/whatever your assets on the fly in production if you're making use of those gems. 

在Gemfile中,我有

 group :assets do gem 'sass-rails', '~> 3.2.3' gem 'coffee-rails', '~> 3.2.1' gem 'uglifier', '>= 1.0.3' end 

这是否意味着app / assets / javascripts / posts.js.coffee文件在部署之前未正确编译,这是问题所在?

非常感谢您的帮助。

如果发生这种情况,请尝试以下方法:

  1. 清除本地环境中的public / assets文件夹中的所有内容。 在rails根目录中,运行rm -rf public/assets
  2. 清除浏览器缓存,因为它可能使用旧资产: press ctrl+F5 or delete your browser history manually
  3. 尝试重新启动服务器a. cap deploy:restart (in your local terminal) AND b. sudo service nginx restart (in your server) a. cap deploy:restart (in your local terminal) AND b. sudo service nginx restart (in your server)
  4. 如果#2和#3还没有工作,现在继续部署。 cap deploy

为了解决这个问题,我了解到:

  1. 在一般情况下,资产不应在本地进行预编译; 它们是在部署期间编译的,因此您不必运行bundle exec rake assets:precompile
  2. 不建议在生产环境中“即时编译”。
  3. 您不必更改config / application.rb中的任何默认设置。
  4. 您不必关闭config.asset.debug来解决此问题。

阅读以下文档以更好地了解资产管道:

Rails / Bundler预编译与延迟编译

http://guides.rubyonrails.org/asset_pipeline.html