Search code examples
javascriptjqueryruby-on-railsproduction-environment

Rails and Javascript in production environment


Hello Stack Overflow community! I am new to Ruby on Rails, and nearing the end of my first project, I am having a little bit of trouble with getting my app production ready.

My first problem was that I needed to refresh the page to get my personal jQuery to work. I fixed this problem by adding the jquery-turbolinks gem to the project and adding jquery.turbolinks to the manifest file.

However, this quick solution did not work for my production environment, for some reason. So I just changed the ordering of some stuff in my manifest file, changed config.serve_static_files to false and config.assets.compile to true (in production.rb). So now none of my JS works in production.

Then, from info gathered from the web, I ran bundle exec rake assets:precompile. Now none of my JS or CSS effects are evident in production!

So finally, to undo that blunder, I ran bundle exec rake assets:clean. Now terminal is complaining that I have "untracked files" when committing changes with git.

Clearly, I'm in an absolutely messy situation. Other than this blunder, my project is running smoothly, so any help would be greatly, greatly appreciated.

PS: Everything works perfectly in development environment...


Solution

  • Welcome to StackOverflow!

    From what you've explained, it seems that running precompile locally didn't quite work, and an attempt to run rake assets:clean has left Git saying there are changes that need to be committed.

    In Sprockets, running rake assets:clean only removes old assets (keeps the most recent 3 copies) from public/assets - [source]. Running this command altered your public/assets directory, and these changes have not been committed, hence the terminal complaining.

    If you wish to wipe public/assets and generate the assets from scratch, you can do the following:

    rake assets:clobber
    RAILS_ENV=production bundle exec rake assets:precompile
    

    Using clobber will destroy the assets directory, while the precompile command following it will rebuild the assets in a way that should reflect your production environment. Once you commit these changes and deploy, your new assets should display properly in production.

    Hope this helps!