Coffeescript/Javascript 无法在服务器上运行

2024-02-28

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

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

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

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

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

UPDATE:

来自 Rails 关于资产管道的指南http://guides.rubyonrails.org/asset_pipeline.html 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.

This https://stackoverflow.com/questions/7673988/rails-bundler-precompile-vs-lazy-compile解释了这一行配置/应用程序.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

这是否意味着应用程序/资产/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)
  4. 如果 #2 和 #3 还不起作用,现在就继续部署。cap deploy

试图解决这个问题,我了解到:

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

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

Rails/Bundler 预编译与延迟编译 https://stackoverflow.com/questions/7673988/rails-bundler-precompile-vs-lazy-compile

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Coffeescript/Javascript 无法在服务器上运行 的相关文章

随机推荐