2

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

我试过在bundle exec rake assets:precompile本地运行并部署它。另外,我也尝试在服务器上运行相同的命令。问题还没有解决。

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

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

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

更新:

来自 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文件在部署之前没有正确编译,这就是问题所在?

非常感谢您的帮助。

4

1 回答 1

0

如果发生这种情况,请尝试以下操作:

  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)
  4. If #2 and #3 didn't work yet, now go ahead and deploy. cap deploy

Trying to solve the problem, I learned:

  1. Assets are not supposed to be precompiled locally in general case; they are compiled during deployment, so you should not have to run bundle exec rake assets:precompile
  2. It's not recommended to "compile on the fly" in production environment.
  3. You do not have to change any default settings in config/application.rb.
  4. You do not have to turn off config.asset.debug to solve this problem.

Read the following documents to understand better about the asset pipeline:

Rails/Bundler precompile vs lazy compile

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

于 2012-12-12T04:51:47.763 回答