Adding A Bootstrap Theme in Ruby on Rails 6

This article will explain how to use a bootstrap theme in a Ruby on Rails 6 application using AdmineLTE3. Start like this:

1
2
rails new bootstrap_theme
rails db:create

Add bootstrap, its dependencies, and your theme.

1
2
yarn add bootstrap jquery popper.js
yarn add admin-lte@3

In config/webpack/environment.js, add the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# config/webpack/environment.js
const { environment } = require("@rails/webpacker");
const webpack = require("webpack");

// Add an additional plugin of your choosing : ProvidePlugin
environment.plugins.prepend(
  "Provide",
  new webpack.ProvidePlugin({
    $: "jquery",
    jQuery: "jquery",
    jquery: "jquery",
    "window.Tether": "tether",
    Popper: ["popper.js", "default"] // for Bootstrap 4
  })
);

module.exports = environment;

Create a new file app/javascript/packs/admin.js.

1
touch app/javascript/packs/admin.js

Make sure it looks like this:

1
2
3
4
5
6
7
8
9
10
# app/javascript/packs/admin.js

var jQuery = require("jquery");

// import jQuery from "jquery";
global.$ = global.jQuery = jQuery;
window.$ = window.jQuery = jQuery;

require("bootstrap");
require("admin-lte");

Generate an Admin controller with an index page.

1
rails g controller admin index

In app/assets/stylesheets/admin.css.scss, add the following lines:

1
2
3
4
5
6
# app/assets/stylesheets/admin.css.scss

@import url("https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700&lang=en");

@import "bootstrap/scss/bootstrap";
@import "admin-lte/dist/css/adminlte";

Create a new layout app/views/layouts/admin.html.erb.

1
touch app/views/layouts/admin.html.erb

In the newly created layout, add the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# app/views/layouts/admin.html.erb

<!DOCTYPE html>
<html>
 <head>
   <title>Admin</title>
   <%= csrf_meta_tags %>
   <%= csp_meta_tag %>

   <%= stylesheet_link_tag 'admin', media: 'all', 'data-turbolinks-track': 'reload' %>
   <%= javascript_pack_tag 'admin', 'data-turbolinks-track': 'reload' %>
   <script src="https://kit.fontawesome.com/6d90f39943.js"></script>
 </head>

 <body class="hold-transition sidebar-mini layout-fixed">
   <div class="wrapper">
     <%= render "layouts/admin_nav" %>
     <%= render "layouts/admin_side_nav" %>
     <%= yield %>
     <%= render "layouts/admin_footer" %>
   </div>
 </body>
</html>

Create three more layouts.

1
2
3
touch app/views/layouts/_admin_nav.html.erb
touch app/views/layouts/_admin_side_nav.html.erb
touch app/views/layouts/_admin_footer.html.erb

In app/views/layouts/_admin_nav.html.erb, put the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# app/views/layouts/_admin_nav.html.erb

<nav class="main-header navbar navbar-expand navbar-white navbar-light">
 <ul class="navbar-nav">
   <li class="nav-item">
     <a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
   </li>
   <li class="nav-item d-none d-sm-inline-block">
     <a href="index3.html" class="nav-link">Home</a>
   </li>
   <li class="nav-item d-none d-sm-inline-block">
     <a href="#" class="nav-link">Contact</a>
   </li>
 </ul>

 <form class="form-inline ml-3">
   <div class="input-group input-group-sm">
     <input class="form-control form-control-navbar" type="search" placeholder="Search" aria-label="Search">
     <div class="input-group-append">
       <button class="btn btn-navbar" type="submit">
         <i class="fas fa-search"></i>
       </button>
     </div>
   </div>
 </form>

 <ul class="navbar-nav ml-auto">
   <li class="nav-item dropdown">
     <a class="nav-link" data-toggle="dropdown" href="#">
       <i class="far fa-comments"></i>
       <span class="badge badge-danger navbar-badge">3</span>
     </a>
     <div class="dropdown-menu dropdown-menu-lg dropdown-menu-right">
       <a href="#" class="dropdown-item">
         <div class="media">
           <img src="dist/img/user1-128x128.jpg" alt="User Avatar" class="img-size-50 mr-3 img-circle">
           <div class="media-body">
             <h3 class="dropdown-item-title">
               Brad Diesel
               <span class="float-right text-sm text-danger"><i class="fas fa-star"></i></span>
             </h3>
             <p class="text-sm">Call me whenever you can...</p>
             <p class="text-sm text-muted"><i class="far fa-clock mr-1"></i> 4 Hours Ago</p>
           </div>
         </div>
       </a>
       <div class="dropdown-divider"></div>
       <a href="#" class="dropdown-item">
         <div class="media">
           <img src="dist/img/user8-128x128.jpg" alt="User Avatar" class="img-size-50 img-circle mr-3">
           <div class="media-body">
             <h3 class="dropdown-item-title">
               John Pierce
               <span class="float-right text-sm text-muted"><i class="fas fa-star"></i></span>
             </h3>
             <p class="text-sm">I got your message bro</p>
             <p class="text-sm text-muted"><i class="far fa-clock mr-1"></i> 4 Hours Ago</p>
           </div>
         </div>
       </a>
       <div class="dropdown-divider"></div>
       <a href="#" class="dropdown-item">
         <div class="media">
           <img src="dist/img/user3-128x128.jpg" alt="User Avatar" class="img-size-50 img-circle mr-3">
           <div class="media-body">
             <h3 class="dropdown-item-title">
               Nora Silvester
               <span class="float-right text-sm text-warning"><i class="fas fa-star"></i></span>
             </h3>
             <p class="text-sm">The subject goes here</p>
             <p class="text-sm text-muted"><i class="far fa-clock mr-1"></i> 4 Hours Ago</p>
           </div>
         </div>
       </a>
       <div class="dropdown-divider"></div>
       <a href="#" class="dropdown-item dropdown-footer">See All Messages</a>
     </div>
   </li>
   <li class="nav-item dropdown">
     <a class="nav-link" data-toggle="dropdown" href="#">
       <i class="far fa-bell"></i>
       <span class="badge badge-warning navbar-badge">15</span>
     </a>
     <div class="dropdown-menu dropdown-menu-lg dropdown-menu-right">
       <span class="dropdown-item dropdown-header">15 Notifications</span>
       <div class="dropdown-divider"></div>
       <a href="#" class="dropdown-item">
         <i class="fas fa-envelope mr-2"></i> 4 new messages
         <span class="float-right text-muted text-sm">3 mins</span>
       </a>
       <div class="dropdown-divider"></div>
       <a href="#" class="dropdown-item">
         <i class="fas fa-users mr-2"></i> 8 friend requests
         <span class="float-right text-muted text-sm">12 hours</span>
       </a>
       <div class="dropdown-divider"></div>
       <a href="#" class="dropdown-item">
         <i class="fas fa-file mr-2"></i> 3 new reports
         <span class="float-right text-muted text-sm">2 days</span>
       </a>
       <div class="dropdown-divider"></div>
       <a href="#" class="dropdown-item dropdown-footer">See All Notifications</a>
     </div>
   </li>
   <li class="nav-item">
     <a class="nav-link" data-widget="control-sidebar" data-slide="true" href="#" role="button">
       <i class="fas fa-th-large"></i>
     </a>
   </li>
 </ul>
</nav>

In app/views/layouts/_admin_side_nav.html.erb, put the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# app/views/layouts/_admin_side_nav.html.erb

<aside class="main-sidebar sidebar-dark-purple-1 elevation-4">
 <!-- Brand Logo -->
 <a href="index3.html" class="brand-link">
   <img src="https://adminlte.io/themes/v3/dist/img/AdminLTELogo.png" alt="AdminLTE Logo" class="brand-image img-circle elevation-3"
        style="opacity: .8">
   <span class="brand-text font-weight-light">AdminLTE 3</span>
 </a>

 <!-- Sidebar -->
 <div class="sidebar">
   <!-- Sidebar user panel (optional) -->
   <div class="user-panel mt-3 pb-3 mb-3 d-flex">
     <div class="image">
       <img src="https://adminlte.io/themes/v3/dist/img/user2-160x160.jpg" class="img-circle elevation-2" alt="User Image">
     </div>
     <div class="info">
       <a href="#" class="d-block">Alexander Pierce</a>
     </div>
   </div>

   <!-- Sidebar Menu -->
   <nav class="mt-2">
     <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
       <!-- Add icons to the links using the .nav-icon class
            with font-awesome or any other icon font library -->
       <li class="nav-item has-treeview menu-open">
         <a href="#" class="nav-link active">
           <i class="nav-icon fas fa-tachometer-alt"></i>
           <p>
             Dashboard
             <i class="right fas fa-angle-left"></i>
           </p>
         </a>
         <ul class="nav nav-treeview">
           <li class="nav-item">
             <a href="./index.html" class="nav-link active">
               <i class="far fa-circle nav-icon"></i>
               <p>Dashboard v1</p>
             </a>
           </li>
           <li class="nav-item">
             <a href="./index2.html" class="nav-link">
               <i class="far fa-circle nav-icon"></i>
               <p>Dashboard v2</p>
             </a>
           </li>
           <li class="nav-item">
             <a href="./index3.html" class="nav-link">
               <i class="far fa-circle nav-icon"></i>
               <p>Dashboard v3</p>
             </a>
           </li>
         </ul>
       </li>
       <li class="nav-item">
         <a href="pages/widgets.html" class="nav-link">
           <i class="nav-icon fas fa-th"></i>
           <p>
             Widgets
             <span class="right badge badge-danger">New</span>
           </p>
         </a>
       </li>
       <li class="nav-item has-treeview">
         <a href="#" class="nav-link">
           <i class="nav-icon fas fa-copy"></i>
           <p>
             Layout Options
             <i class="fas fa-angle-left right"></i>
             <span class="badge badge-info right">6</span>
           </p>
         </a>
         <ul class="nav nav-treeview">
           <li class="nav-item">
             <a href="pages/layout/top-nav.html" class="nav-link">
               <i class="far fa-circle nav-icon"></i>
               <p>Top Navigation</p>
             </a>
           </li>
           <li class="nav-item">
             <a href="pages/layout/top-nav-sidebar.html" class="nav-link">
               <i class="far fa-circle nav-icon"></i>
               <p>Top Navigation + Sidebar</p>
             </a>
           </li>
           <li class="nav-item">
             <a href="pages/layout/boxed.html" class="nav-link">
               <i class="far fa-circle nav-icon"></i>
               <p>Boxed</p>
             </a>
           </li>
           <li class="nav-item">
             <a href="pages/layout/fixed-sidebar.html" class="nav-link">
               <i class="far fa-circle nav-icon"></i>
               <p>Fixed Sidebar</p>
             </a>
           </li>
           <li class="nav-item">
             <a href="pages/layout/fixed-topnav.html" class="nav-link">
               <i class="far fa-circle nav-icon"></i>
               <p>Fixed Navbar</p>
             </a>
           </li>
           <li class="nav-item">
             <a href="pages/layout/fixed-footer.html" class="nav-link">
               <i class="far fa-circle nav-icon"></i>
               <p>Fixed Footer</p>
             </a>
           </li>
           <li class="nav-item">
             <a href="pages/layout/collapsed-sidebar.html" class="nav-link">
               <i class="far fa-circle nav-icon"></i>
               <p>Collapsed Sidebar</p>
             </a>
           </li>
         </ul>
       </li>
     </ul>
   </nav>
   <!-- /.sidebar-menu -->
 </div>
 <!-- /.sidebar -->
</aside>

In app/views/layouts/_admin_footer.html.erb, put the following code.

1
2
3
4
5
6
7
8
9
# app/views/layouts/_admin_footer.html.erb

<footer class="main-footer">
  <strong>Copyright &copy; 2014-2019 <a href="http://adminlte.io">AdminLTE.io</a>.</strong>
  All rights reserved.
  <div class="float-right d-none d-sm-inline-block">
    <b>Version</b> 3.0.5
  </div>
</footer>

In app/views/admin/index.html.erb, put the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# app/views/admin/index.html.erb

<div class="content-wrapper">
 <div class="content-header">
   <div class="container-fluid">
     <div class="row mb-2">
       <div class="col-sm-6">
         <h1 class="m-0 text-dark">Dashboard</h1>
       </div><!-- /.col -->
       <div class="col-sm-6">
         <ol class="breadcrumb float-sm-right">
           <li class="breadcrumb-item"><a href="#">Home</a></li>
           <li class="breadcrumb-item active">Dashboard v1</li>
         </ol>
       </div>
     </div>
   </div>
 </div>

 <section class="content">
   <div class="container-fluid">
     <div class="row">
       <div class="col-lg-3 col-6">
         <div class="small-box bg-info">
           <div class="inner">
             <h3>150</h3>
             <p>New Orders</p>
           </div>
           <div class="icon">
             <i class="ion ion-bag"></i>
           </div>
           <a href="#" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
         </div>
       </div>
       <div class="col-lg-3 col-6">
         <div class="small-box bg-success">
           <div class="inner">
             <h3>53<sup style="font-size: 20px">%</sup></h3>
             <p>Bounce Rate</p>
           </div>
           <div class="icon">
             <i class="ion ion-stats-bars"></i>
           </div>
           <a href="#" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
         </div>
       </div>
       <div class="col-lg-3 col-6">
         <div class="small-box bg-warning">
           <div class="inner">
             <h3>44</h3>
             <p>User Registrations</p>
           </div>
           <div class="icon">
             <i class="ion ion-person-add"></i>
           </div>
           <a href="#" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
         </div>
       </div>
       <div class="col-lg-3 col-6">
         <div class="small-box bg-danger">
           <div class="inner">
             <h3>65</h3>
             <p>Unique Visitors</p>
           </div>
           <div class="icon">
             <i class="ion ion-pie-graph"></i>
           </div>
           <a href="#" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
         </div>
       </div>
     </div>
   </div>
 </section>
</div>

You have now successfully integrated the admin-lte theme in a Ruby on Rails 6 application.