Coffeescript / Javascript不是在服务器上工作 [英] Coffeescript/Javascript Is Not Working on Server

查看:144
本文介绍了Coffeescript / Javascript不是在服务器上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了无尽滚动功能,如果有更多的帖子要显示,用户可以使用它来保持向下滚动。我按照Railscasts,它的工作伟大的本地(javascripts和意愿分页宝石)。
但是,在服务器上,此功能不工作。我所看到的只是简单的分页,并且不应用无限滚动。
我认为它与编译或预处理相关,因为javascript在本地运行良好。



我试过运行 bundle exec rake assets:预编译在本地,并进行部署。
此外,我试图在服务器上运行相同的命令。



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


  1. app / assets / javascripts / posts.js.coffee

  2. app / views / index.js.erb

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



UPDATE:



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



当请求这些文件(coffescripts)时,它们由提供的处理器
由咖啡脚本和sass gem,然后分别作为JavaScript和CSS发送回浏览器

说明 config / application.rb

中的行

  Bundler.require * Rails.groups(:assets =>%w(开发测试))

只从开发和测试环境中的资产组加载宝石。
这意味着像sass-rails和uglifier这样的东西在生产中将不可用
,这意味着你不能正确地
compile / minify /无论你的资产如果你使用这些宝石,生产中的飞翔

在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 文件在部署之前未正确编译,这是问题所在? / p>

非常感谢您的帮助。

解决方案

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


  1. 清除您的本地环境中的public / assets文件夹中的所有内容。在您的rails根目录中,运行 rm -rf public / assets

  2. 清除浏览器缓存,因为它可能使用您的旧资产:按ctrl + F5或手动删除您的浏览器历史记录

  3. 尝试重新启动服务器 cap deploy:重新启动(在本地终端)和b。 sudo service nginx restart(在您的服务器中)

  4. 如果#2和#3还没有工作,现在继续部署。 cap deploy

尝试解决问题,我学到了:


  1. 资产不应在一般情况下在本地进行预编译;它们是在部署期间编译的,因此您不必运行 bundle exec rake assets:precompile

  2. 不建议

  3. 您不必更改 config / application.rb 中的任何默认设置。
  4. li>您不必关闭 config.asset.debug 即可解决此问题。

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



Rails / Bundler预编译VS lazy编译



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


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). 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.

I've tried running bundle exec rake assets:precompile locally, and deploying it. 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
  2. app/views/index.js.erb

Assume the contents in the js files are fine because the feature works greatly on the local server. 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

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

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

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?

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. In your rails root, run 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
  3. Try restart 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. 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

这篇关于Coffeescript / Javascript不是在服务器上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆