Last Updated: February 25, 2016
·
917
· neydroid

Setting up a great team, Rails and Bower

One thing I always try to do is select the best tool for the job on hand, in this case I think Bower is the best handling the assets and dependencies for my Rails apps.

First make sure that you have node.js and bower installed before going forward.

Second, for Rails it all begins with the gem bower-rails. We need to include this to our Gemfile:

gem "bower-rails", "~> 0.9.1"

After we run bundle we need to choose how to initialize the gem:

Generating a bower.json

rails g bower_rails:initialize json

Or generating a Ruby DSL file named Bowerfile

rails g bower_rails:initialize

I prefer the second option, this option will generate a Bowerfile like this:

asset "angular"
asset "d3"
asset "fontawesome", "4.2.0"

By default the gem will put the files in the path ./vendor/assets/bower_components and also will add some rake tasks to wrap bower commands like:

rake bower:install
rake bower:update

We need to include the path in our config/application.rb with:

config.assets.paths << Rails.root.join("vendor","assets","bower_components")

After that we can require the assets in our application.css or application.js like:

//= require d3/src/d3

Have fun!

2 Responses
Add your response

over 1 year ago ·

Thanks @mmd-coding for the link! I've heard of it but I haven't used it yet, I'll try it. It looks cleaner.

over 1 year ago ·