问题
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:
- app/assets/javascripts/posts.js.coffee
- 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.
回答1:
If this happens, try the followings:
- Clear everything from public/assets folder in your local environment. In your rails root, run
rm -rf public/assets - Clear out your browser cache, as it might be using your old assets:
press ctrl+F5 or delete your browser history manually - Try restart your server
a. cap deploy:restart (in your local terminal) AND b. sudo service nginx restart (in your server) - If #2 and #3 didn't work yet, now go ahead and deploy.
cap deploy
Trying to solve the problem, I learned:
- 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 - It's not recommended to "compile on the fly" in production environment.
- You do not have to change any default settings in config/application.rb.
- 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
来源:https://stackoverflow.com/questions/13832470/coffeescript-javascript-is-not-working-on-server