Installing Bootstrap 3 on Rails App
I'm trying to install Bootstrap 3.0 on my Rails app. I recently finished Michael Hartl's tutorial and am now trying to build my own system using this new version of Bootstrap, but I have a few questions that I'm not sure about.
My system specs:
- OS X Mountain Lion on MBP
- Rails 4.0
- Ruby 2.0
Questions I have:
- What is the best gem to use in my Gemfile? I have found a few of them.
- What do I import on my
custom.css.scss
? I read somewhere that it's different from 2.3.2. - Is there anything else I have to do to get Bootstrap to work, or are the remaining steps identical to the ones I followed for Bootstrap 2.3.2?
Edit
Here is what the bootstrap-rails project on GitHub first says to do:
gem 'anjlab-bootstrap-rails', :require => 'bootstrap-rails',
:github => 'anjlab/bootstrap-rails'
Then it says to do:
gem 'anjlab-bootstrap-rails', '>= 3.0.0.0', :require => 'bootstrap-rails'
Do they do the same thing, or do you have to do them both?
Solution 1:
Actually you don't need gem for this, here is the step to install Bootstrap 3 in RoR
Download Bootstrap
-
Copy:
bootstrap-dist/css/bootstrap.css
andbootstrap-dist/css/bootstrap.min.css
To:
vendor/assets/stylesheets
-
Copy:
bootstrap-dist/js/bootstrap.js
andbootstrap-dist/js/bootstrap.min.js
To:
vendor/assets/javascripts
-
Update:
app/assets/stylesheets/application.css
by adding:*= require bootstrap.min
-
Update:
app/assets/javascripts/application.js
by adding://= require bootstrap.min
With this you can update bootstrap any time you want, don't need to wait gem to be updated. Also with this approach assets pipeline will use minified versions in production.
Solution 2:
As many know, there is no need for a gem.
Steps to take:
- Download Bootstrap
- Direct download link Bootstrap 3.1.1
- Or got to http://getbootstrap.com/
-
Copy
bootstrap/dist/css/bootstrap.css bootstrap/dist/css/bootstrap.min.css
to:
app/assets/stylesheets
-
Copy
bootstrap/dist/js/bootstrap.js bootstrap/dist/js/bootstrap.min.js
to:
app/assets/javascripts
-
Append to:
app/assets/stylesheets/application.css
*= require bootstrap
-
Append to:
app/assets/javascripts/application.js
//= require bootstrap
That is all. You are ready to add a new cool Bootstrap template.
Why app/
instead of vendor/
?
It is important to add the files to app/assets, so in the future you'll be able to overwrite Bootstrap styles.
If later you want to add a custom.css.scss
file with custom styles. You'll have something similar to this in application.css
:
*= require bootstrap
*= require custom
If you placed the bootstrap files in app/assets, everything works as expected. But, if you placed them in vendor/assets, the Bootstrap files will be loaded last. Like this:
<link href="/assets/custom.css?body=1" media="screen" rel="stylesheet">
<link href="/assets/bootstrap.css?body=1" media="screen" rel="stylesheet">
So, some of your customizations won't be used as the Bootstrap styles will override them.
Reason behind this
Rails will search for assets in many locations; to get a list of this locations you can do this:
$ rails console
> Rails.application.config.assets.paths
In the output you'll see that app/assets takes precedence, thus loading it first.