Using Bootstrap 4 In Rails 6
Tags: rails, ruby, css
Published: 2020-06-18 05:14:29

Using Bootstrap in a Rails app can be very confusing at first to help make things easier and smoother and allow you to go back to developing, I'm going to try and help you get started with Bootstrap.
We are going to start from a point where you already have a rails app up as far as (views, controllers, models, and some data ).

###*Getting Started*

You first need to head on over to https://github.com/twbs/bootstrap-rubygem and grab these 2 gems as shown below. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/le90z9rxn4t13nx4fafv.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8b1e18zog9arhxo4apse.png)

once you've copied them take them over to your gemfile which if you havent used yet I will provide a screenshot of the location.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ya70fn3mbal0yi9cprx0.png)


Go ahead and paste them and save the file or hit "cmd + s"

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qkqdwv6nkk8pc3b42yzc.png)

Switch over to your terminal make sure you are in the right directory for your app go ahead and type 'bundle' or 'bundle install'. This will add the gems to your app and add it to your gemfile lock, don't worry too much about it for now but what it's doing is locking in the version of your gems you have installed so you don't have to worry about  gem updates messing up your app. It may take a while to load but just be patient :).


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fy0fx7prypimcsbw9579.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/503pxn6enuwxfkjmegk9.png)

Once done there's still a couple more steps we need to take before we are all setup. Next you need to go to your 
app/assets/stylesheets/application.scss if it's a .css file go ahead and make it a .scss but it should default to .scss

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ih75n6yymnc6q7stx6wu.png)

Inside the application.scss go ahead and paste.


```ruby
@import "bootstrap";
```


Next we need to go to our app/javascript/packs/application.js folder and paste the code below. After you've done so you're ready to build the next Twitter! well not just yet, I'm going to show you how to use it. 
```ruby
//= require jquery3
//= require popper
//= require bootstrap-sprockets
```



###*Using Bootstrap*


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xoub6wso8afozji8j4ha.png)

How do we go from the picture above to the picture below. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qz0vdybs9ndllqv38ml6.png)

Most importantly making the page take in user input and storing that instance into our Database. I'm going to be showing you how to make a simple sign-up page that not only looks super neat but works. Once again I'm taking into account you have a working form and your controllers and routes are working and focus solely on making it pop!

Rails has some pretty cool helpers to ease making a functional form I'm going to be linking it below!

What these Helpers will do is help us by writing out some of that HTML for us, so let's get started. 

Let's say we are making a form_for a @user to signup our initial syntax may look like 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zwwnw56uugjl7bwhdatj.png)


This is where Bootstrap comes in to save the day. We are going to be using one of Bootstraps classes to help us 


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/omkyzrqr62mpzandpoqs.png)

So then we will begin to add some of the attributes that are required for this form in my instance it was a couple of things but this can vary for you. So how do we keep this layout consistent for all the attributes. Bootstrap and Rail helpers make this extremely easy, like so. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8toskcimrc1pfun8hlr6.png)

There's a lot going on here but if we break it down it's really clear to understand.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v8jy1wcn3gxdelmkg4wc.png)

So our f.label and f.text_field is us using our Rails helpers to let our controller know this is what is going to be entered here. Next to it is us letting bootstrap know we are still using their form and next is just a place holder that will delete once you begin typing.  {class:"form-control", placeholder:"Enter name"} 

It may seem like a lot but 1) Our Rails helper is just making our HTML easier to write. 2) The Bootstrap is helping us with our CSS and HTML. If we inspect our Page you can see the same name form being rendered into plain HTML.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kzzfov4g5s9sqd57q7p3.png)

Once done you can end your form with a simple button provided by Bootstrap. Once submitted it will enter the data into your database working way better than a simple HTML form. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e59tf9ozxux2w81cd53w.png)

I'm going to place some links below to the Bootstrap documentation which I highly encourage you to read and play around with and watch your application come to life. 


https://guides.rubyonrails.org/form_helpers.html

https://getbootstrap.com/docs/4.5/getting-started/introduction/