You have a responsibility to look after the owner of your business

You are a separate entity who has relationships, family, friends, needs, wants and dreams — and not all of those things are business related. When business is failing we see ourselves as the failure…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Add Bootstrap 4 to a Rails 5 app in 8 Steps

Everything we need to get Bootstrap 4 working is in the section above.

I found it very confusing the first time I tried to implement it, the documentation is essentially one section running into the next. So this blog walks through it, one step at a time.

Open your Rails application in Atom. Take a look at the list of files and folders in your app:

Down towards the bottom, we can see a file called ‘Gemfile’. Open it, and here’s what we see:

This file contains all of the gems used in your Rails application. So when we want to add another gem, here’s where we go to add it! Let’s do that now:

Copy the code below to your ‘Gemfile’.

gem 'bootstrap', '~> 4.0.0'

Save the file!

Step 1 done!

Don’t worry too much about what sprockets-rails is, we just need to make sure that we’ve got version 2.3.2 or higher in our Rails app. To do this, go to Terminal and run bundle install (type bundle install then hit the enter key). The output will end with something like this:

If your look closely, you can find the line that mentions sprockets-rails. It says Using sprockets-rails 3.2.1 . That’s a higher number than 2.3.2, so we’re good.

Next step!

The instructions are to:

a) bundle install; and

b) restart your server

Let’s do that.

a) bundle install: Like in step 2.

b) restart your server:

First, we find the file application.scss. The file path we’re given tells us where to find it — we go into app, then assets, then stylesheets, and that’s where we should find the file we’re looking for. Let’s take a look:

We have an application.css, which is close to what we want, but the extension name is wrong — it should be scss. We will deal with that in the next step. This is the file we want. Copy and paste:

@import "bootstrap";

into the file and save.

Step 4 Complete.

Basically, we need to rename our file so that it’s a scss file rather than a cssfile.

Change the css extension to scss.
You should have a file that looks the the above.

Step 5 Complete!

All we need to do here is remove all the lines in our newly renamed application.scss file that start with *=require:

Your application.scss file should look like this.

Save the file and you’ve done all you need to for this step.

If the version of Rails you’re using is 5.1 or above, you’ll need to add the gem to your Gemfile.

Copy the following code to your Gemfile and save the file.

gem 'jquery-rails'

Run bundle install and restart the Rails server after adding a gem to the Gemfile, so it’s a good time to do both of those things now.

Onto the final step!

Now we need to find the application.js file and add some lines to it. We can go with either the first option or the second. We’ll go with the first:

Paste the lines.

And that’s it, we’re done! Time to start adding Bootstrap to your views!

Having followed every step exactly as described. Let’s add some simple Bootstrap code to one of our views and see if it works:

The Unstyled Movies Index View
Add the button code from the bootstrap 4.0 page on line 31.
This is how the button looks. We need to move the link to create a new movie into the button.
The code from line 32 is inserted into line 31 to replace ‘BUTTON TITLE’.

It worked! By adding a a Bootstrap class, we can see that Bootstrap is being applied correctly in our Rails app.

Integration Complete.

Add a comment

Related posts:

Magazine Comparison

This task focussed on identifying desktop publishing elements between two articles, one of a lower and one of a higher quality. The task was to identify and comment on these differences. I chose to…

Interning at WhizKey

This summer I got to intern at one of the most futuristic companies, WhizKey Future Labs, Dubai! The company provides solutions and customizable products related to artificial intelligence…

Teaching Resources in Social Innovation

In the midst of the summer heatwave in Charlotte NC, with our sabbatical plans to South Africa canceled, we redirected our energy to relaunch the Social Change Innovators 2.0 website while writing a…