Rails - cannot run app on Heroku because of uncompiled assets - ruby-on-rails

When I run the app I deployed to Heroku, I get this error:
ActionView::Template::Error ('fontawesome.less' wasn't found
I've tried precompile the assets locally - this way:
bundle exec rake assets:precompile
The result:
...
FATAL: database "myapp_production" does not exist
...
When I run - heroku run rake assets:precompile, the result is:
...
'fontawesome.less' wasn't found
...
The setup in config/environments/production.rb:
config.assets.compile = true
What causes this problem?
Many thanks

You should resolve the database config issue to be able to compile assets locally.
The assets compiled will all be .css, not less.
Check config/application.rb, I for example have these lines there:
config.assets.paths << Rails.root.join("app", "assets", "flash")
config.assets.precompile += [ 'print.css', 'resume.css', 'application.css', 'reset-min.css', 'lightbox.css', 'bootstrap.css', 'grid.css' ]
config.assets.precompile += [ 'rails.js', 'application.js', 'bootstrap.js' ]
config.assets.enabled = true
config.assets.version = '1.0'

You need to have less-rails gem and the sufix of the file is .css.less
gem 'less-rails'
If you require the file separately of the application.js, you need to add the precompile array in environments/production.rb, and the extension of the file should be ".css.less"
config.assets.precompile += %w( fontawesome.css )
And be sure of you have enable the static_assets option in environments/production.rb
config.serve_static_assets = true

Related

Precompilation breaking on heroku, works locally

I did some restructuring of our less files. Everything works locally (e.g. rake assets:precompile), but on Heroku my push fails with the following:
Running: rake assets:precompile
adding placeholder
rake aborted!
Less::ParseError: variable #brand-primary is undefined
(in /tmp/build_24298d78-579f-44a3-ae43-c4d82b9dde9d/app/assets/stylesheets/lectures/lectures.less)
at /tmp/build_24298d78-579f-44a3-ae43-c4d82b9dde9d/vendor/bundle/ruby/2.1.0/gems/less-2.5.1/lib/less/js/lib/less/parser.js:604:31
After much poking around, I decided to remove the import statement for the offending file (lectures.less) to see what breaks next. With the change committed and pushed to Github, I tried pushing again to Heroku, and got the exact same error -- the precompilation is now breaking on a file that shouldn't be imported any more.
Details pasted below; anyone got any tips? I've tried both heroku run rake assets:clean and heroku run rake tmp:clear, but I didn't expect them to work anyway.
My application.css is barebones:
...
* You're free to add application-wide styles to this file and they'll appear at the top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require main
*/
PREVIOUSLY I was requiring the lectures file and some others in application.css, but moved it into the main.less.
My main.less has the rest of the imports:
#import "settings";
#import "variables";
...
#import "lectures/lectures.less";
...
Relevant environment settings:
development.rb
config.assets.precompile += %w( admin.js admin.css )
config.less.dumpLineNumbers = 'all'
config.assets.debug = true
config.serve_static_assets = true
config.assets.compile = true
config.assets.raise_runtime_errors = true
staging.rb
config.assets.precompile += %w( admin.js admin.css )
config.serve_static_assets = true
config.assets.compile = false
config.assets.digest = true
config.assets.version = '1.0'
relevant gems:
ruby '2.1.2'
gem 'rails', '>= 4'
gem 'less-rails', github: 'metaskills/less-rails'
We appeared to have fixed the issue by bumping the config.assets.version setting in production.rb, e.g.
- config.assets.version = '1.0'
+ config.assets.version = '1.1'
It's not clear in the slightest to me why heroku didn't recognize the changes and why heroku run rake assets:clean didn't do it, but there you go.
EDIT #################
It's been a while since this, and to keep from having the issue on a regular basis, we wound up tying the version # to our commit sha. Something like this:
whatever.rb
heroku = Heroku::API.new(api_key: ENV['HEROKU_API_KEY'])
$asset_version = heroku.get_releases(ENV["HEROKU_APP_NAME"]).body[-1]["commit"]
production.rb
config.assets.version = $asset_version

Rails Assets: Custom Application.css.scss

I know that in order to precompile your assets you do the following
bundle exec rake assets:precompile
which will take your application.css.sass in app/assets/styesheets and generate application-xxxxx.css in your public/assets directory
My question, what if you have a custom application_custom.css.scss How to precompile it to be used by production?
You can include that file in the application.css.scss manifest file.
Add the following line in your app/assets/stylesheets/application.css.scss:
*= require application_custom
You can also specify additional assets to precompile by adding them to config.assets.precompile += %w( ... ) in your environment configuration, e.g. config/environments/production.rb

Heroku does not precompile assets when deploying

It very clearly says in its documentation that it would do this if I don't precompile them locally.
And truthfully, I have no interest in precompiling these locally.
What I've had in production.rb, I've duplicated in application.rb
In my production.rb :
config.serve_static_assets = false
config.assets.compile = false
config.assets.precompile << 'application.js'
config.assets.precompile << 'application.css'
config.assets.precompile << 'screen.css'
Then I deploy, and that returns :
-----> Compiled slug size: 52.4MB
-----> Launching... done, v28
http://myapp.herokuapp.com deployed to Heroku
So it "compiled" something, right? Except no go, I go to the site and the .css and .js files are blank.
In order to precompile this locally, I am required to comment out in bootstraps_and_overrides.css the line :
#import "screen.css.scss";
Then it precompiles locally, and my local machine will not load the css correctly, but remotely it will actually work correctly.
So my method of deployment now is comment out that line of code,
bundle exec rake assets:precompile
git add .
git commit -m "Adding public/assets"
git push heroku development:master
Then ( unfortunately! ) :
bundle exec rake assets:clean
And then uncomment that line of code in my .css.
Some things to check
You're on Cedar, right? Heroku only supports this behavior on Cedar.
You're on Rails 3, right? Heroku doesn't support precompiling assets on Rails 4.
You have asset pipeline turned on, right? in config/application.rb you need the line config.assets.enabled = true
You have to not have a public/assets folder. On deployment Heroku decides whether or not to precompile based on whether this folder is present (even if it's empty).
If the pipeline is on and you don't have an assets folder, then pre-compilation must be failing.
Try changing to this. I hope this will help you.
In config/environments/production.rb
config.assets.compile = true
config.assets.digest = true
You might be on the wrong Heroku stack. Make sure you specify stack Cedar when you create apps that use the asset pipeline.
heroku create -stack cedar
The reason it would not deploy was because of Google fonts. Moving the file to your application.css such as :
*= require_self
*= require_tree .
*/
#import url(http://fonts.googleapis.com/css?family=Special+Elite);
Will allow the app to deploy, and for the fonts to work.

Why is this new CSS manifest file not being compiled during Heroku slug compilation, while the normal application manifest works fine?

I have a Rails 3.2.11 running on the Heroku Cedar stack. I don't compile assets locally, they are compiled automatically during slug compilation, as described here. I have just created a new CSS manifest file app/assets/stylesheets/new_manifest.css.scss in addition to the default application.css.scss. When I push to Heroku, this manifest file is not getting compiled like application.css.scss is. The new manifest file works fine in development. Why would that be happening?
application.rb
config.assets.enabled = true
config.assets.version = '1.0'
config.assets.initialize_on_precompile = false
production.rb
config.assets.compile = false
config.serve_static_assets = false
config.assets.compress = true
config.assets.digest = true
Console output from Heroku push
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
Asset precompilation completed (83.62s)
Sample page
<%= stylesheet_link_tag "new_manifest", media: "all" %>
The solution was to add config.assets.precompile += %w( new_manifest.css ) to production.rb, as described here. This isn't required in development, but is required in production, which caused my confusion.
If you say your new_manifest is a manifest file , it should be included in the main manifest for the application : application.css . In case you are including it by stylesheet_link_tag it is not under asset-pipeline control (it is treated like a pure CSS file , not as a manifest one). It is about to be precompiled also , but inclusions are about to be ignored . There is a more proper way to include additional manifest files into the main application.css : index files(look for 2.1.2 ) .

How do I precompile assets in rails in development environment?

I want to manually precompile some assets in my dev environment (to check the content of the compiled js and css files) from the command line.
I am running
RAILS_ENV=development bundle exec rake assets:precompile
but my js and css are not compiled in the public/assets folder, only the images are. Any idea why that may be happening?
Try adding this to your config/environments/production.rb :
config.assets.precompile += %w( *.css *.js )

Resources