【问题标题】:Coffeescript/Javascript Is Not Working on ServerCoffeescript/Javascript 在服务器上不工作
【发布时间】:2012-11-29 17:26:45
【问题描述】:

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

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

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

  1. app/assets/javascripts/posts.js.coffee
  2. app/views/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.

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

非常感谢您的热心帮助。

【问题讨论】:

  • 如果问题如您所想,您会在错误日志中找到相关信息。
  • 我应该如何开始检查/调试这个问题?
  • 部署、跟踪日志并加载出现意外行为的页面。观察你记录的错误。
  • @Deefour 你能检查我的更新吗?
  • 当您运行 rake assets:precompile 时,app/assets/javascripts/posts.js.coffee 被编译为原始 javascript,然后通过 Sprockets //= require ... 行附加到您的 app/assets/application.js 文件中。编译/连接的application.js 在部署之前放入public/assets

标签: javascript ruby-on-rails compilation coffeescript


【解决方案1】:

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

  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. 您不必更改 config/application.rb 中的任何默认设置。
  4. 您不必关闭 config.asset.debug 即可解决此问题。

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

Rails/Bundler precompile vs lazy compile

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 2011-12-18
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    相关资源
    最近更新 更多