Rails 4 assets.precompile

2020-05-26 11:47发布

问题:

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?

回答1:

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.



回答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.