What's the best way to install bootstrap with a Rails app?


Shade abu el makarin

I am new to Rails, so I would like to ask how to use Rails with Bootstrap? Before learning backend development, I used to call it simply at the beginning of the html tag of the html file like this:

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

So I am wondering what is the correct way in rails? Should I do the same thing and put these calls in the head of the html tags in the app layout view file (except jQuery, since it's already implemented)?

Because if it's that simple, then why do people make special gems for bootstraps?

I'm sure there's a lot of points I'm missing or don't know, so I'd appreciate some clarification and guidance.

PS: The Rails app already exists, I just want to use bootstrap to tweak the design.

mark

Why install the bootstrap gem?

Since Rails uses the asset pipeline to use the sass version of bootstrap to minimize and condense stylesheets, javascript and images are the preferred way to include bootstrap in app layout views rather than just bootstrap. Also, if you only include bootstrap in a header file, that included file must be a compiled version of bootstrap (it's just a CSS file). However, since we will be including a sass version of bootstrap in your app, you can access bootstrap's sass variables, mixins, and other sass related functionality. You can't get that functionality by including compiled assets in the app layout view. By importing sass into the application.scss file, Rails can compile your bootstrap and assets on the fly and give you more flexibility in designing your application.

Add Bootstrap to Rails app

According to the bootstrap-sass gem , you need to add

'gem 'bootstrap-sass'

to your Gemfile, then run

bundle install

Next, you will want to import the bootstrap stylesheets in your application css manifest file. However, by default the manifest file is named:

app/assets/stylesheets/application.css

But you should rename it to use the .scss extension (or .sass extension) like this:

app/assets/stylesheets/application.scss

Now, delete everything in the application.scss file and add the following two lines:

@import "bootstrap-sprockets";
@import "bootstrap";

From now on you will need to handle the import of scss files manually.

Next, to make bootstrap's javascript helper available, you need to add the following line:

//= require bootstrap-sprockets

for you

app/assets/javascripts/application.js

You need to add this line so your application.js file looks like this:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory,   lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require turbolinks
//= require_tree .

Related


What's the best way to install bootstrap with a Rails app?

Shade abu el makarin I am new to Rails, so I would like to ask how to use Rails in Bootstrap? Before learning backend development, I used to call it simply at the beginning of the html tag of the html file like this: <!-- Latest compiled and minified CSS --> <

What's the best way to install bootstrap with a Rails app?

Shade abu el makarin I am new to Rails, so I would like to ask how to use Rails in Bootstrap? Before learning backend development, I used to call it simply at the beginning of the html tag of the html file like this: <!-- Latest compiled and minified CSS --> <

What's the best way to install bootstrap with a Rails app?

Shade abu el makarin I am new to Rails, so I would like to ask how to use Rails in Bootstrap? Before learning backend development, I used to call it simply at the beginning of the html tag of the html file like this: <!-- Latest compiled and minified CSS --> <

What's the best way to install Angular on Rails 4?

Ji Lee I have populated the api with Rails and now want to mount a frontend framework like Angular on Rails. I've seen people use Gulp or Grunt to do this, but still not 100%. Would love to see how the experts stack Angular and Rails together effectively. Grat

What's the best way to install Angular on Rails 4?

Ji Lee I have populated my api with Rails and now want to install a frontend framework like Angular on Rails. I've seen people use Gulp or Grunt to do this, but still not 100%. Would love to see how the experts stack Angular and Rails together effectively. Gra

What's the best way to install Angular on Rails 4?

Ji Lee I have populated my api with Rails and now want to install a frontend framework like Angular on Rails. I've seen people use Gulp or Grunt to do this, but still not 100%. Would love to see how the experts stack Angular and Rails together effectively. Gra

What's the best way to install Angular on Rails 4?

Ji Lee I have populated the api with Rails and now want to mount a frontend framework like Angular on Rails. I've seen people use Gulp or Grunt to do this, but still not 100%. Would love to see how the experts stack Angular and Rails together effectively. Very

What's the best way to replicate Rails code?

turk So, I have developed the front end of the database. The controller has all 7 standard actions and many more. The view consists of Index, Edit, New, etc. Now I need to do 3 more, actually the only difference between them is one row, because I'm making a SQ

What's the best way to replicate Rails code?

turk So, I have developed the front end of the database. The controller has all 7 standard actions and many more. The view consists of Index, Edit, New, etc. Now I need to do 3 more, actually the only difference between them is one row, because I'm making a SQ

What's the best way to create navigation in an app?

John Coffey I would like to know what is the best practice regarding navigation on an iOS app. As far as I know, you can do this in three ways: navigation bar, tab bar or custom buttons. Each of them has advantages and disadvantages, but I'm wondering what's t

What's the best way to create navigation in an app?

John Coffey I would like to know what is the best practice regarding navigation on an iOS app. As far as I know, you can do this in three ways: navigation bar, tab bar or custom buttons. Each of them has advantages and disadvantages, but I'm wondering what's t

What's the best way to install Conda on MacOS (Apple/Mac)?

Joe What is the recommended way to install Anaconda on a Mac? brew cask install anaconda I came back after trying for a while anaconda was successfully installed!. After that, try condathe command to return command not found: conda. Is there any post-installat

What's the best way to install Node and npm on Mac?

one I have a new Mac. I want to install Node and npm on it. I want to control the versions of Node and npm that I install and uninstall on my system. What is the most appropriate way to achieve this? Joao Marcelo Brito I like NVM . You can install it using cur

What's the best way to install Conda on MacOS (Apple/Mac)?

Joe What is the recommended way to install Anaconda on a Mac? brew cask install anaconda I came back after trying for a while anaconda was successfully installed!. After that, try condathe command to return command not found: conda. Is there any post-installat

What's the best way to set up a fresh OS install?

Matt My computer is slow so I'm reinstalling Windows 7. However, I would like to know the best way to set up the filesystem. Previously, I just installed and used it. A friend told me that I should put the OS on a separate partition and should not store Steam

What's the best way to lobby in install/upgrade scripts?

Zohini Sometimes I name install/upgrade scripts in custom modules with any of the names mentioned below: install-0.1.0.0.php upgrade-0.1.0.0-0.1.1.0.php upgrade-0.1.1.0-0.1.2.0.php ---------------------------- install-0.1.0.php upgrade-0.1.0.0-0.1.1.php upgr

What's the best way to install Node.js on OSX?

Rolando I see you can download nodejspass homepageor install via homebrewor via macports. Which method is best? Is there a reason to choose one? Some things I consider: 1. Ease of upgrading to latest version 2. Ability to (fully) remove and uninstall 3. 1 and

What's the best way to install Node.js on OSX?

Rolando I see you can download nodejspass homepageor install via homebrewor via macports. Which method is best? Is there a reason to choose one? Some things I consider: 1. Ease of upgrading to latest version 2. Ability to (fully) remove and uninstall 3. 1 and

What's the best way to install Unity and KDE together?

crazy I am installing 16.04 from scratch. I want to use both Unity and KDE on my system. Is it better to install Ubuntu and add Plasma Desktop, or should I choose Kubuntu and add Unity? tramp Both methods work fine. KDE is just another skin for Ubuntu, and so

What's the best way to install Unity and KDE together?

crazy I am installing 16.04 from scratch. I want to use both Unity and KDE on my system. Is it better to install Ubuntu and add Plasma Desktop, or should I choose Kubuntu and add Unity? tramp Both methods work fine. KDE is just another skin for Ubuntu, and so

What's the best way to install Conda on MacOS (Apple/Mac)?

Joe What is the recommended way to install Anaconda on a Mac? brew cask install anaconda I came back after trying for a while anaconda was successfully installed!. After that, try condathe command to return command not found: conda. Is there any post-installat

What's the best way to lobby in install/upgrade scripts?

Zohini Sometimes I name install/upgrade scripts in custom modules with any of the names mentioned below: install-0.1.0.0.php upgrade-0.1.0.0-0.1.1.0.php upgrade-0.1.1.0-0.1.2.0.php ---------------------------- install-0.1.0.php upgrade-0.1.0.0-0.1.1.php upgr

What's the best way to set up a fresh OS install?

Matt My computer is slow so I'm reinstalling Windows 7. However, I would like to know the best way to set up the filesystem. Previously, I just installed and used it. A friend told me that I should put the OS on a separate partition and should not store Steam

What's the best way to install Unity and KDE together?

crazy I am installing 16.04 from scratch. I want to use both Unity and KDE on my system. Is it better to install Ubuntu and add Plasma Desktop, or should I choose Kubuntu and add Unity? tramp Both methods work fine. KDE is just another skin for Ubuntu, and so

What's the best way to get Bootstrap popover content using Ajax?

Aycolmbro I'm developing a WordPress plugin that displays a "special button" on the front end via a WordPress shortcode. I'm using Bootstrap 4, the buttons use Bootstrap popovers, I need to retrieve/get the popover title and content from the database without r

What's the best way to add some space after the Bootstrap navbar?

GibboK The following code always displays the navigation bar at the top of the page. I need to place the second container (content) at the end of the navbar instead of below it. Currently, the second container is below the navbar. I could add some white space

What's the best way to declare a bootstrap-vue button?

A_L According to the bootstrap-vue documentation, a button is created like this: <b-button variant="success" >Button</b-button> But the standard bootstrap syntax also works and renders the same way: <button type="button" class="btn btn-success">Button</button