Rails 3.2 asset compilation issues on Heroku - ruby-on-rails

It seems a lot of people are having similar problems. I consulted this thread, this, and this article, among others. I have found a lot of topics concerning these issues, but the posted solutions have been unable to find a definitive fix. I am currently running rails 3.2.8 with ruby 1.9.2, and Heroku stack cedar.
I am having two major, related issues:
1) My projects are pushing some assets - not all - to heroku.
Per suggestions in other threads, when I place:
config.assets.initialize_on_precompile = false
in my /config/application.rb,
config.serve_static_assets = true
config.assets.precompile += %w( *.js )
in /config/environments/production.rb, and
gem 'rails_12factor', group: :production
in my gemfile, the push will precompile successfully, however the application.js file is empty on Heroku:
2) Precompiling before a push successfully puts JS on Heroku, but it is no longer recognized.
After removing
config.assets.precompile += %w( *.js )
from /config/production.rb, and running
RAILS_ENV=production bundle exec rake assets:precompile
committing and pushing results with index.html.erb not recognizing JS despite application.js present & non-empty as shown below:
I feel as if I have tried all combinations of these solutions to no avail. I have not yet updated the applications to Rails 4, is this a good idea to do at this time?
Any other suggestions where to go from here?
Oddly, in another Rails 3.2 app of mine, I get a similar error only the first time I visit
Once the page loads, simply hitting refresh loads all content and the errors go away. This app has none of the attempted solutions above in its codebase.

I had the same problem, what solved it for me was specifying my ruby version in the gemfile.
ruby '1.9.3'

No answer, but this may help. I precompile using the options below, which means only MD5 versions are created, this helped me fix a few issues.
rake -v --trace assets:precompile:primary RAILS_ENV=production
I also always do this before a precompile:
rake assets:clean
Also, double-check the exact MD5'd name, both in the html and the name of the file in heroku.

Same happened to me. When I updated the Rails version from 3.2.8 to 3.2.11 it Worked!

Related

Rails 3.2 Asset Precompile is skipping files

Stack
Ruby 1.9.3
Rails 3.2.12
Issue
We have precompilation issues with our application.
We're trying to remove config.server_static_files = true and config.assets.compile = true from our production configs, but are constantly missing assets whenever we precompiled. Even though these assets live in app/assets, while other files in the same directory are seen and compiled.
Ex:
app/assets/stylesheets/application.css
app/assets/stylesheets/some_slider_lib.css
app/assets/stylesheets/bootstrap.min.css
After precompiling
public/assets/application-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.css
public/assets/some_slider_lib-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.css
And the manifest.yml is also missing the bootstrap file among other assets.
Same thing with our vendor folder.
Attempts
The following is the only way that I've managed to precompiles everything.
config.assets.precompile += ["*.css", "*.scss", "*.js", "*.coffee"]
However, this feels like overkill. I thought all files within the asset pipeline would be compiled. Is this not what is suppose to happen?
Also tried rake assets:precompile --trace, but this was unhelpful.
If anyone could provide a little guidance, suggestions or more it would be deeply appreciated.
Thanks!

Rails 4: Devise.secret_key was not set

This seems to be a popular issue, since I found many similar questions on Stack Overflow:
Devise Token Auth error: Devise.secret_key was not set
Devise Secret Key was not set
Mina and Devise.secret_key was not set
However, none of the above seems to help fix my problem.
Here is my situation:
I installed Font Awesome with the font-awesome-rails gem, and it was working locally, but whenever I deployed to Heroku, Font Awesome icons were no longer appearing.
So, I looked for answers to fix this problem, and found out that I needed to run RAILS_ENV=production bundle exec rake assets:precompile to make things work.
However, when I run this command, I get the following error in Terminal:
rake aborted!
Devise.secret_key was not set. Please add the following to your Devise initializer:
config.secret_key = '—— MY_SECRET_KEY_HERE ———'
Please ensure you restarted your application after installing Devise or setting the key.
As per the solutions offered in the questions mentioned at the beginning of my question, I do haveconfig.secret_key = ENV['DEVISE_SECRET_KEY'] if Rails.env.production? in devise.rb
Also, as recommended here, I did run heroku config:set DEVISE_SECRET_KEY='—— MY_SECRET_KEY_HERE ———' -- app name_of_my_app_here.
Because I was not sure, I also ran heroku config:set DEVISE_SECRET_KEY=—— MY_SECRET_KEY_HERE ——— -- app name_of_my_app_here (without single quotes around my secret key).
But none of these things worked.
I keep getting the same error.
What am I doing wrong?
I found a solution to this problem.
Instead of keeping on struggling with the font-awesome-rails gem, I uninstalled it and replaced it with font-awesome-sass, which is the official Font Awesome gem for rails.
Following the guidelines offered here (section: EASY: SASS Ruby Gem), I then ran bundle and added:
#import "font-awesome-sprockets";
#import "font-awesome";
to my application.scss file.
I also removed the old #import "font-awesome.css"; line from the very same file.
Now, Font Awesome works both locally and on Heroku.

Heroku asset compile not working with Rails 3.2 application

I feel like I have exhausted my resources trying to figure this out. I have yet to run into this error with a heroku/rails app before and am simply puzzled as to why my js/css isnt being compiled/uploaded to Heroku.
As stated in the heroku docs your options are to:
1. Compiling assets locally.
2. Compiling assets during slug compilation.
I tried the first option: ran RAILS_ENV=production bundle exec rake assets:precompile, added public/assets and saw the "Detected manifest.yml, assuming assets were compiled locally" line when deploying. No CSS.
I tried the second option (clean git checkout before assets:precompile). Added the 'config.assets.initialize_on_precompile = false' line as suggested for rails 3.x apps. And instead of seeing the expected:
Compiled jquery.js
Compiled jquery_ujs.js
Compiled application.js
Compiled application.css
etc etc
I get 'DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins!' warnings. yet that folder is completely empty
Looked into this warning with no sign of it being the root cause for this. Feels like I'm going in circles so I am hoping someone has insight. Thanks in advance!
Heroku docs suggests that add 'rails_12factor' gem to gemfile. Precompling assets before pushing to heroku is not required. And you also must use Heroku's cedar stack only.

Precompiling Assets Failure During Deployment

I am using Capistrano to precompile my assets during deployment, and I am getting an error that isn't very useful to me.
* executing "cd -- path/to/releases/20130507161214 && bundle exec rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile"
*** rake aborted!
*** wrong argument type nil (expected Data)
*** (in /path/to/app/assets/javascripts/application.js)
*** /path/to/gems/therubyracer-0.11.4/lib/v8/context.rb:67: in 'New'
Here is my application.js that is referred:
//= require jquery
//= require jquery_ujs
//= require bootstrap
Update
So I tried to just remove application.js entirely and run it again, but then it complained about another js file of mine (but the same non-helpful error). Then I removed all of my js files and it started to complain about my css files.
If all js and css files are removed, the precompile does succeed (but obviously without my needed js and css files).
I am using .js.coffee and .css.scss for all of my non-image assets.
Update
#TomDavies answer did indeed help me, but still having the issue. During deployment, it was just failing on precompile and not giving me information. So I did what Tom mentioned, I precompiled on my development machine, but in the production environment, and noticed I was also having issues.
The error it gave me was "Unexpected INDENT", but it would not tell me a line number, just a file name...
I eventually discovered I was missing a comma in my ajax request. Apparently it works without it in the development environment, just doesn't precompile without it...
Here is what I had:
$.ajax 'some/url'
success:
alert 'yay!'
Here is what I needed:
$.ajax 'some/url',
success:
alert 'yay!'
Notice the comma at the end of the top line... now precompiles great in development. I thought for sure that fixing the errors during precompile on the development machine (but production environment) would fix the deployment issue, but to no avail. It is still failing with the same error at the top wrong argument type nil (expected Data).
tl;dr
I am able to run the rake assets:precompile RAILS_ENV=production on my development machine, doesn't work on the production machine, gives the error at the top. Not sure how to debug this one.
This looks like a possible gem problem, since it's failing on the application.js file and your content looks correct. Which version of Ruby are you working with? If you have switched different versions, you may want to clear out your gems directory and reinstall them from scratch. I have experienced issues with gems installed under one Ruby version leaving fodder behind that causes strange things to happen under another version.
Your file should be called application.scss if you want to import other scss from it.
I'm not sure of you exact error, but for asset pipeline issues you should always try compiling them locally in development. The errors are usually much more obvious that way and you can trial / error more easily. Here are some steps I usually leave in my config/environments/development.rb for getting asset precompilation to work locally:
# NOTE: To test asset pipeline in dev uncomment the following configs and run:
#
# $ RAILS_ENV=development rake assets:precompile
#
# It is recommended you rm -rf public/assets when you are finished or this may affect
# your local dev environment
#
# config.assets.compress = true
# config.assets.compile = false
# config.assets.digest = true
And here is a gist of the same:
https://gist.github.com/atomgiant/5438672
I would also look to using Node.js as the ExecJS runtime. There are several documented issues with TheRubyRacer and memory management (https://github.com/cowboyd/therubyracer/issues/350) and it seems that the project has been abandoned (https://github.com/cowboyd/therubyracer/pull/348).
Assuming you have the latest Node.js installed, you can prepend/append EXECJS_RUNTIME="Node" to your precompile task:
rake assets:precompile RAILS_ENV=production EXECJS_RUNTIME="Node"
As a bonus, this sped up asset compilation by an order of magnitude.

Rails precompile - all.js missing

I am new to rails and using version 3.1.0 and trying to deploy to heroku. I have a very simple application that i have managed to get up and running on heroku after working my way through several issues. I used the command
bundle exec rake assets:precompile
to get heroku to load the assets for the application (i read that heroku requires this for version 3.1.0). The problem is that when i try to run the application, the server gives the error
ActionController::RoutingError (No route matches [GET] "/assets/all.js"):
i have all of the required javascript files in public/javscripts but i cannot find any version of all.js in the assets directory. I have included
config.assets.precompile << '*.js'
in config/application.rb but to no avail.
I was wondering if someone could tell me how I get the precompile to create all.js in the assets directory or if I am completely off track here. i was under the impression that the precompile function compiled all of the javascript files into a single optimized file.
Also, if anyone has any good links for a beginner to understand how the precompile functionality works in rails I would be very appreciative.
Thanks in advance.
The best guide is the official rails guide. There is also the Railscast about it.
In a nutshell your Javascript files go in app/assets/javascript from where the precompile task will process them and put then in the public/assets directory. You should not have to change the precompile config option if the follow the defaults. Don't forget to the use the correct helpers to reference your files.
Read the resources above, and if you are converting an existing app watch the Railscast and follow the last section of the guide - these should point you in the right direction.

Resources