Daniel Friis
Daniel Friis

Reputation: 484

config.assets.precompile not working

I have a bookmarklet, which needs separate js and css files - i.e. I don't want them compiled as usual into one file.

These are the files that I want to separate from the usual js and css files:

assets/javascripts/bookmarklet/tagger.js
assets/javascripts/bookmarklet/js/bookmarklet.js
assets/stylesheets/bookmarklet/bookmarklet-frame.css

In application.js and application.css I changed //= require_three . to //= require_directory .

And I added:

config.assets.precompile += [ 'bookmarklet/js/bookmarklet.js', 'bookmarklet/tagger.js', 'bookmarklet/bookmarklet-frame.css' ]

to my production.rb and staging.rb.

An application.js and application.css is generated, however, my three separate files are not. Any idea why this is not working?

Upvotes: 1

Views: 2184

Answers (3)

Kiran A
Kiran A

Reputation: 59

Another solution to consider where moving to application.rb may not be necessary. Instead, add 'production' to :assets in your config/application.rb. 'production' is by default not included in the list.

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test production)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

Upvotes: 1

Daniel Friis
Daniel Friis

Reputation: 484

Had to move

config.assets.precompile += [ 'bookmarklet/js/bookmarklet.js', 'bookmarklet/tagger.js', 'bookmarklet/bookmarklet-frame.css' ]

to application.rb.

Upvotes: 2

Vysakh Sreenivasan
Vysakh Sreenivasan

Reputation: 1729

Make sure you have require_tree instead of require_directory, require_tree adds recursively but require_directory does not. Refer asset pipeline

application.js

//= require jquery
//= require jquery_ujs
//= require_tree .

application.css

/* ...
*= require_self
*= require_tree .
*/

application.html.erb should have these lines

<%= javascript_include_tag "application" %>
<%= stylesheet_link_tag "application" %>

Now when you precompile the assets with command RAILS_ENV=production bundle exec rake assets:precompile in your production you will have(all the js files compiled into one, css into one)

application.html.erb

<script src="/assets/application-908e25f4bf641868d8683022a5b62f54.js"></script>
<link href="/assets/application-4dd5b109ee3439da54f5bdfd78a80473.css" media="screen" rel="stylesheet" />

This is unnecessary as by default application.js, application.css are precompiled.

config.assets.precompile += [ 'bookmarklet/js/bookmarklet.js', 'bookmarklet/tagger.js', 'bookmarklet/bookmarklet-frame.css' ]

Sometimes you may use admin.js for a layout admin.html.erb only then you need to add a line like this in production.rb

Upvotes: 2

Related Questions