2

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.

Community
  • 1
  • 1
Maximus S
  • 10,759
  • 19
  • 75
  • 154
  • If the issue is like you are thinking it is, you'll find information in your error logs. – deefour Dec 12 '12 at 04:03
  • How should I start checking/debugging this problem? – Maximus S Dec 12 '12 at 04:06
  • Deploy, tail your logs, and load a page that is experiencing the unexpected behavior. Watch you logs for an error. – deefour Dec 12 '12 at 04:16
  • @Deefour could you check my update please? – Maximus S Dec 12 '12 at 04:22
  • When you ran `rake assets:precompile`, `app/assets/javascripts/posts.js.coffee` is compiled to raw javascript and is then appended to something like your `app/assets/application.js` file through a Sprockets `//= require ...` line. The compiled/concatenated `application.js` is put into `public/assets` before your deploy. – deefour Dec 12 '12 at 04:33
  • Thank you! I managed to solve the problem. – Maximus S Dec 12 '12 at 04:40

1 Answers1

0

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

Community
  • 1
  • 1
Maximus S
  • 10,759
  • 19
  • 75
  • 154