precompile coffeescript files ( Rails 4 ) - ruby-on-rails

In my Rails 4 app I have a users.js.coffee file in app/assets/javascripts. It compiles and works fine in development. But when I deploy to heroku it's not working. According to Railsguides:
The matcher (and other members of the precompile array; see below) is applied to final compiled file names. This means anything that compiles to JS/CSS is excluded, as well as raw JS/CSS files; for example, .coffee and .scss files are not automatically included as they compile to JS/CSS.
So I added the following line to my config/environments/production.rb
config.assets.precompile += %w ( users.js.coffee )
It's still not forcing the file to be precompiled. Do you know how I may force Rails to precompile it (I use RAILS_ENV=production bundle exec rake assets:precompile to precompile)?
Thanks a lot.

Your config.assets.precompile should contain users.js, not users.js.coffee.

Related

rails asset pipeline production precompile

I have a directory projName/vendor/assets/bootstrap/css/
I am in production mode.
production.rb contains: config.assets.precompile << /(^[^_\/]|\/[^_])[^\/]*$/
when I run rake assets:precompile
I get
projName/public/assets/css/ but I want projName/public/assets/bootstrap/css/
I do not understand why the bootstrap directory is not there.
Actually all the top level directories under vendor/assets and app/assets are missing in public assets/
Compiled assets are written to the location specified in config.assets.prefix. By default, this is the public/assets directory.
To understand this you have to first understand what precompiling is and does. Let me explain
When you run (a rake task)
rake assets:precompile
it will create a public folder inside your application folder which will compile all your asset manifests (ie. your application.css and application.js)
How exactly does this happen ?? -> Rails comes bundled with a rake task which will compile all this. That rake task is what is shown above.
Compiled assets are written to the location specified in config.assets.prefix. By default, this is the public/assets directory.
The default matcher for compiling files includes application.js, application.css and all non-JS/CSS files (this will include all image assets automatically) from app/assets folders including your gems.
And thats exactly what that regex means (to include everything in your app/assets folder), you can also explicitly provide it as shown in the answer above.
Hope this helped.
Here are few links for your reference
http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
http://dennisreimann.de/blog/precompiling-rails-assets-for-development/
What does that regex mean? If you want to precompile everything, try this.
config.assets.precompile = ['*.js', '*.css']
Actually, if you want to precompile everything, try this:
def precompile?(path)
%w(app lib vendor).each do |asset_root|
assets_path = Rails.root.join(asset_root, 'assets').to_path
return true if path.starts_with?(assets_path)
end
false
end
# Precompile all assets under app/assets (unless they start with _)
Rails.application.config.assets.precompile << proc do |name, path|
starts_with_underscore = name.split('/').last.starts_with?('_')
unless starts_with_underscore
path = Rails.application.assets.resolve(name).to_path unless path # Rails 4 passes path; Rails 3 doesn't
precompile?(path)
end
end
(Based on the code in the Rails Guide.)

Rails asset pipeline - JS and CoffeeScript

I have both .js and .coffee files in my /app/assets/javascripts/ folder. The .coffee files will not run unless I call rake assets:precompile, which is a pain because I have to do rake assets:clean and precompile them again whenever I make a change.
Also, the precompiled .js file is included in addition to the source files, which causes double event handlers and all that good stuff.
My understanding is that the coffeescript should be compiled to javascript upon each request if it's not precompiled, but it doesn't seem to be doing so. I can't find the compiled script loading in Firebug, and I don't see its behavior, at least.
My /config/application.rb has the following line configured:
# Enable the asset pipeline
config.assets.enabled = true
What else is there to check?
I am using Rails 3.2.3.
If you precompile on your local machine, then you can commit these generated assets into the repository and proceed with deployment. No need to compile them on production machine.
But it introduces a problem: now when you change source files (coffescript / scss), the app won't pick up the changes, because it will serve precompiled files instead. rake assets:clean deletes these precompiled files.
from https://stackoverflow.com/a/9335864/643500
What I usually do if I want the assets to precompile on the production server to pickup the new changes every build is just clean the assets - once of course unless you re-precompile them
rake assets:clean
When the changes are made and you don't want to precompile them every build do
rake assets:clean
rake assets:precompile

Why does rails precompile task do non-digest assets

When I do:
rake assets:precompile RAILS_ENV=production
I get for example the following files in my public/assets folder:
application-7af6c31514bcdd4cce3c96892af4487f.js
application-7af6c31514bcdd4cce3c96892af4487f.js.gz
application.js
application.js.gz
The last 2 are a problem because it causes the compiled version to get served in development and I don't understand why they are being generated.
I have the following line in my production.rb:
config.assets.digest = true
To stop the creation of the non-hashed filenames in public use:
rake assets:precompile:primary RAILS_ENV=production
This is the normal behaviour of the asset compiler, the non-digest files are generated mainly for use in error pages and the like (where you don't have access to the MD5 hash) and there is currently no way to turn them off. Also, this question is virtually identical to this one: Rails compiles assets both with and without md5 hash, why?

Sass Not Precompiling on Rails 3.1 Asset Pipeline, Assets:precompile rake task

I've got an app I just converted to Rails 3.1. In my app/assets/stylesheets I have a bunch of sass files that use #import to import parial sass files. One of these files is called screen.sass and it's the main sass file that I want to use in one of my layouts.
However, when I run
RAILS_ENV=demo bundle exec rake assets:precompile
I get that screen isn't precompiled
(in ../app/assets/stylesheets/screen.sass)
It wasn't getting compiled at all with no message without adding this to demo.rb
config.assets.precompile += %w( screen.css )
I've tried several things like renaming the screen.sass file to screen.css.sass and I've tried creating a manifest file called screen.css in my app/assets/stylesheets directory and requiring the screen.sass file. In each case I get the same precompile error for it as above.
I'm not precompiling with the production environment because demo is my staging environment I want to test the 3.1 upgrade on.
Anybody have any ideas as to what else I can try to fix this error?
rake assets precompile needs to know what type of file to compile your stylesheet to. Change screen.sass to screen.css.sass

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