Layouts in Ruby on Rails

Often there are situations where there are separate menus/layouts for admin and other pages. This article will discuss layouts in Ruby on Rails. To start:

1
rails new layouts
1
2
rails generate controller pages index
rails generate controller admin index

Add style to app/assets/stylesheets/pages.scss:

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
.topnav {
 background-color: #333;
 overflow: hidden;
}

/* Style the links inside the navigation bar */
.topnav a {
 float: left;
 color: #f2f2f2;
 text-align: center;
 padding: 14px 16px;
 text-decoration: none;
 font-size: 17px;
}

/* Change the color of links on hover */
.topnav a:hover {
 background-color: #ddd;
 color: black;
}

/* Add a color to the active/current link */
.topnav a.active {
 background-color: #4CAF50;
 color: white;
}

Import pages.scss in application.css.scss

1
@import "pages";

Make sure your app/views/layouts/application.html.erb looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html>
 <head>
   <title>LayoutsArticle</title>
   <%= csrf_meta_tags %>
   <%= csp_meta_tag %>

   <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
   <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
 </head>

 <body>
   <div class="topnav">
     <a class="active" href="#home">Home</a>
     <a href="#news">News</a>
     <a href="#contact">Contact</a>
     <a href="#about">About</a>
   </div>

   <%= yield %>
 </body>
</html>

Now go to your http://localhost:3000/pages/index, which should look something like this:

Screen Shot 2020-05-05 at 2.36.41 AM.png

Now to add a layout for admins. Add style to app/assets/stylesheets/admin.scss:

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
.sidenav {
 height: 100%;
 width: 160px;
 position: fixed;
 z-index: 1;
 top: 0;
 left: 0;
 background-color: #111;
 overflow-x: hidden;
 padding-top: 20px;
 a {
   padding: 6px 8px 6px 16px;
   text-decoration: none;
   font-size: 25px;
   color: #818181;
   display: block;
   &:hover {
     color: #f1f1f1;
   }
 }
}
.main {
 margin-left: 160px;
 padding: 0px 10px;
}
@media screen and (max-height: 450px) {
 .sidenav {
   padding-top: 15px;
   a {
     font-size: 18px;
   }
 }
}

Import admin.scss in application.css.scss:

1
2
@import "pages";
@import "admin";

Create app/views/layouts/admin.html.erb and make sure it looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<html>
 <head>
   <title>LayoutsArticle</title>
   <%= csrf_meta_tags %>
   <%= csp_meta_tag %>

   <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
   <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
 </head>

 <body>
   <div class="sidenav">
     <a href="#">About</a>
     <a href="#">Services</a>
     <a href="#">Clients</a>
     <a href="#">Contact</a>
   </div>

   <div class="main">
     <%= yield %>
   </div>
 </body>
</html>

Now go to http://localhost:3000/admin/index, which should look something like this:

Screen Shot 2020-05-05 at 2.44.45 AM.png

There you have it!