12

This is a common question in here, but none of the solutions fixed my problem, so here it goes:

I am adding ace.js to my rails4 app, So what I did was

- Added vendor/assets/ace/ace.js
- Created vendor/assets/ace/index.js , with content
  //= require ace  
- Added the following to my production.rb
  config.assets.precompile += %w( index.js )  
  config.assets.paths << Rails.root.join("vendor", "assets", "ace")

So in my layout file I have:

<%= javascript_include_tag "ace" %>

and it works just fine on dev, but when I run:

RAILS_ENV=production bundle exec rake assets:precompile

It doest not create the digest version of the ace file.

Am I missing something?

Arthur Neves
  • 11,840
  • 8
  • 60
  • 73

2 Answers2

22

It is solved by adding :

config.assets.precompile += %w( index.js )

to config/application.rb. (not config/environments/production.rb)

Tested in Rails 4.0 beta1.

DuoSRX
  • 4,179
  • 3
  • 26
  • 32
ccq1170
  • 244
  • 1
  • 4
  • 4
    Thanks this really helped me. Was this a change in Rails 4? It users to be in `production.rb` but the docs aren't clear in pointing out that this is now supposed to be set in `application.rb' – cman77 Jan 06 '14 at 19:57
  • 1
    Is there a way to do this without specifying every individual file? – ahnbizcad Nov 03 '14 at 05:18
  • 1
    @ahnbizcad This *might* work: `config.assets.precompile += [/(^[^_\/]|\/[^_])[^\/]*$/]` – dhulihan May 26 '15 at 23:28
2

In case you are adding the ace directory as an assets , you should place the js files under vendor/assets/ace/javascripts directory (which you should create) .

Normally you don't need config.assets.precompile += %w( index.js ), Sprockets will manage it just fine. If there is just one js file to add from vendors/assets/ace directory , you don't need an index.js there . The index.js would be proper if you have a lot of subdirectories and files under vendor/assets/ace/javascripts/your_vendor_js_library . One last touch : you don't need <%= javascript_include_tag "ace" %> , it duplicates <%= javascript_include_tag "application" %>'s content.

R Milushev
  • 4,295
  • 3
  • 27
  • 35
  • So when I precompile, will it create a application-xxx.js and ace-xxx.js files? – Arthur Neves Feb 14 '13 at 19:45
  • I think the file will be only one - `application-xxx.css` .That's the reason behind precompilling assets. – R Milushev Feb 14 '13 at 19:49
  • The idea for combining the assets in one optimized file is to decrease server hits. What would be the reason for splitting assets ?[A nice reference about the theory](http://guides.rubyonrails.org/asset_pipeline.html). – R Milushev Feb 14 '13 at 19:53
  • 3
    I need to slip them, because the ace.js is big and will be used in 10$ of the screens only. – Arthur Neves Feb 14 '13 at 20:05
  • You can load assets per controller , it it fits in your case. – R Milushev Feb 14 '13 at 20:37