Coffeescript / Javascript在服务器上不起作用
我已经实现了“无限滚动”function,如果有更多post要显示,用户可以使用该function继续向下滚动。 我遵循Railscasts,它在本地工作得很好(javascripts和will-paginate gem)。 但是,在服务器上,此function不起作用。 我所看到的只是简单的分页,并且不会应用无限滚动。 我认为这与编译或预处理有关,因为javascript在本地工作正常。
我已经尝试运行bundle exec rake assets:precompile
在本地bundle exec rake assets:precompile
并部署它。 此外,我尝试在服务器上运行相同的命令。 问题尚未解决。
有没有人对这个问题有一个很好的解释? 相关文件如下:
- 应用程序/资产/ JavaScript的/ posts.js.coffee
- 应用/视图/ index.js.erb的
假设js文件中的内容很好,因为该function在本地服务器上运行很大。 我几乎可以肯定问题的根源是编译。
更新:
来自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文件在部署之前未正确编译,这是问题所在?
非常感谢您的帮助。
如果发生这种情况,请尝试以下方法:
- 清除本地环境中的public / assets文件夹中的所有内容。 在rails根目录中,运行
rm -rf public/assets
- 清除浏览器缓存,因为它可能使用旧资产:
press ctrl+F5 or delete your browser history manually
- 尝试重新启动服务器
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)
- 如果#2和#3还没有工作,现在继续部署。
cap deploy
为了解决这个问题,我了解到:
- 在一般情况下,资产不应在本地进行预编译; 它们是在部署期间编译的,因此您不必运行
bundle exec rake assets:precompile
- 不建议在生产环境中“即时编译”。
- 您不必更改config / application.rb中的任何默认设置。
- 您不必关闭config.asset.debug来解决此问题。
阅读以下文档以更好地了解资产管道:
Rails / Bundler预编译与延迟编译
- 在Rails Devise app中Find_for_database_authentication vs Find_by?
- 为什么我的Heroku应用程序没有使用Sendgrid在生产中发送电子邮件?
- 在coffeescript中获取rails视图辅助方法
- Rails / Javascript:如何将rails变量注入(非常)简单的javascript中
- 将Rails应用程序部署到Heroku时出现语法错误:ExecJS :: RuntimeError:SyntaxError:意外字符
- 使用把手迭代javascript对象
- Marionette CompositeView为Collection中的每个Model而不是ItemView(Marionette Rails)渲染自己
- Dojo + Rails 3.2.8 + CoffeeScript
- 使用CoffeeScript以编程方式检查复选框
- 条带令牌未携带到控制器导轨4
- 在rails资产管道中的js.coffee文件中使用erb时出错