简体   繁体   English

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

[英]Coffeescript/Javascript Is Not Working on Server

I've implemented 'endless scrolling' feature, which users use to keep scrolling down if there are more posts to show. 我已经实现了“无限滚动”功能,如果有更多帖子要显示,用户可以使用该功能持续向下滚动。 I followed Railscasts, and it works great locally (javascripts and will-paginate gem). 我遵循了Railscasts,在本地效果很好(javascript和will-paginate gem)。 However, on the server, this feature is not working. 但是,在服务器上,此功能不起作用。 All I see is simple pagination, and endless scrolling is not applied. 我所看到的只是简单的分页,并且没有应用无限滚动。 I think it's related to compiling or preprocessing because javascript is working fine locally. 我认为这与编译或预处理有关,因为javascript在本地运行良好。

I've tried running bundle exec rake assets:precompile locally, and deploying it. 我试过运行bundle exec rake assets:precompile在本地bundle exec rake assets:precompile并部署它。 Also, I tried running the same command on the server as well. 另外,我也尝试在服务器上运行相同的命令。 The problem hasn't been solved yet. 该问题尚未解决。

Does anybody have a good explanation for the problem? 有人对这个问题有很好的解释吗? Related files are located as follows: 相关文件位于以下位置:

  1. app/assets/javascripts/posts.js.coffee app / assets / javascripts / posts.js.coffee
  2. app/views/index.js.erb app / views / index.js.erb

Assume the contents in the js files are fine because the feature works greatly on the local server. 假定js文件中的内容很好,因为该功能在本地服务器上可以很好地工作。 I am almost sure that the source of problem is compilation. 我几乎可以确定问题的根源是编译。

UPDATE: 更新:

from Rails guide about assets pipeline http://guides.rubyonrails.org/asset_pipeline.html 从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 explains about the line in config/application.rb 说明了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.

and in Gemfile, I have 在Gemfile中,我有

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

Does this mean that app/assets/javascripts/posts.js.coffee file wasn't compiled properly before being deployed and that was the problem? 这是否意味着app / assets / javascripts / posts.js.coffee文件在部署之前未正确编译,这就是问题所在?

Thank you very much in advance for your kind help. 预先非常感谢您的帮助。

If this happens, try the followings: 如果发生这种情况,请尝试以下操作:

  1. Clear everything from public/assets folder in your local environment. 清除本地环境中public / assets文件夹中的所有内容。 In your rails root, run rm -rf public/assets 在您的rails根目录中,运行rm -rf public/assets
  2. Clear out your browser cache, as it might be using your old assets: press ctrl+F5 or delete your browser history manually 清除浏览器缓存,因为它可能正在使用旧资产: press ctrl+F5 or delete your browser history manually
  3. Try restart your server 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) 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. 如果#2和#3仍无法正常工作,请继续进行部署。 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 它们是在部署期间编译的,因此您不必运行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 . 您不必在config / application.rb中更改任何默认设置。
  4. You do not have to turn off config.asset.debug to solve this problem. 您不必关闭config.asset.debug即可解决此问题。

Read the following documents to understand better about the asset pipeline: 阅读以下文档以更好地了解资产管道:

Rails/Bundler precompile vs lazy compile Rails / Bundler预编译与延迟编译

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM