【发布时间】:2012-11-29 17:26:45
【问题描述】:
我已经实现了“无限滚动”功能,如果有更多帖子要显示,用户可以使用该功能继续向下滚动。我关注了 Railscasts,它在本地运行良好(javascripts 和 will-paginate gem)。 但是,在服务器上,此功能不起作用。我所看到的只是简单的分页,并且没有应用无限滚动。 我认为这与编译或预处理有关,因为 javascript 在本地运行良好。
我尝试在本地运行 bundle exec rake assets:precompile 并进行部署。
另外,我也尝试在服务器上运行相同的命令。问题还没有解决。
有人对这个问题有很好的解释吗?相关文件位于:
- app/assets/javascripts/posts.js.coffee
- 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