This was tested using Ruby 2.0.0 and Rails 4.0.0.rc1.
-
Set the install directory for Bower components:
// .bowerrc { "directory": "app/assets/components" }
-
Follow the Bower instructions and create a
bower.json
file in your directory with your dependencies, e.g.// bower.json { // ... "dependencies": { "d3": "~3.1.0", "underscore": "~1.4.4", } }
-
Include them in your JavaScript:
# config/application.rb # include Bower components in compiled assets config.assets.paths << Rails.root.join('app', 'assets', 'components')
// /Users/aidan/dev/code_cruise/app/assets/javascripts/application.js // // Bower packages //= require d3/d3 //= require underscore/underscore // //= require jquery //= require jquery_ujs // ...
At this point, make sure your app runs and the JS libs installed by Bower are loading on the page.
To have Heroku deploys (Cedar stack only) install and compile Bower components on push:
-
Use a custom Heroku buildpack that includes Node.js and Bower:
heroku config:set BUILDPACK_URL='git://github.com/qnyp/heroku-buildpack-ruby-bower.git#run-bower'
-
Add the following to Rakefile before the
require
:# Rakefile namespace :assets do desc "A little hack to make sprockets handle Bower files" # Add extensions to all Bower-installed files that don't have them. # See https://github.com/sstephenson/sprockets/issues/347 task :precompile do Dir['app/assets/components/**/*'].each do |filename| if File.file?(filename) && File.extname(filename) == '' File.rename(filename, "#{filename}.txt") end end end end require File.expand_path('../config/application', __FILE__) # ...
-
Change the following setting to allow assets to be compiled in production (I don't believe this is Bower-specific):
# config/environments/production.rb config.assets.compile = true
Try a deploy: it should successfully compile assets.
It's just that easy!!! ::facepalm::
Additional resources: