Skip to content

Instantly share code, notes, and snippets.

@normanrs
Last active December 8, 2018 22:40
Show Gist options
  • Save normanrs/c6bf4cf59a67d6c8b6ed5fb4a4920fad to your computer and use it in GitHub Desktop.
Save normanrs/c6bf4cf59a67d6c8b6ed5fb4a4920fad to your computer and use it in GitHub Desktop.
Bootstrap in Rails

Basic Setup of Bootstrap in Rails

by Norm Schultz

ORIENTATION

Working from an existing Rails project, find these two files (you ARE going to need them): ​ /app/assets/stylesheets/application.css #This is your fallback css file, used for customization ​ /app/views/layouts/application.html.erb #This is your default html page setup, used for all pages

SETUP

Replace your application.html.erb code with the one I've supplied below.

EXPLANATION

CDN: The lines with hrefs to .css and .js files represent using Bootstrap via a partnered Content Deliver Network (CDN) instead of hosting the Bootstrap setup files locally. This is advantageous because you can be confident you are up-to-date. Also, you don't have to ensure local files are available in the asset pipeline.

Navbar: The lines within the navbar class represent Boostrap navbar syntax, giving you a default navbar that includes a dropdown. Handy!

Jumbotron: The Bootstrap jumbotron represents a framed container with fairly nice fonts and a rounded-corner background. The placement of the yield ERB means that all your app pages will be rendered in the jumbotron.

CUSTOMIZATION

You can customize your html to take advantage of Bootstrap elements, as well as your application.css file for styling. You will want to cusomize the navbar (obviously). You will want to move the navbar code into your individual app views if you want to display different navbar options depending on the view.

In any view you have access to numerous other handy Bootstrap functions. I've found their grid and card systems to be the most useful. Their grid allows for automatic sizing and for columns divisible by 12. Cards are useful if you want a more floating look with individual borders. (Both of these are described in the "Components" section of the official Boostrap site, linked below.)

REFERENCES

https://getbootstrap.com/docs/3.3/getting-started/

https://www.taniarascia.com/what-is-bootstrap-and-how-do-i-use-it/

https://www.w3schools.com/bootstrap/

https://getbootstrap.com/docs/4.0/components/alerts/

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Template</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-inverse navbar-static-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Balance Web Development</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li><a href="#">About</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Services<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Design</a></li>
<li><a href="#">Development</a></li>
<li><a href="#">Consulting</a></li>
</ul>
</li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</div>
</nav>
<div class="jumbotron">
<div class="container">
<%= yield %>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment