Skip to content

08 Layout and Style

Dave Strus edited this page Jul 18, 2015 · 2 revisions

Layout, Style, and Flash Helper

Let's add helpers to print flash messages:

app/helpers/application_helper.rb

module ApplicationHelper
  def flash_messages(flash)
    flash.map do |name, msg|
      content_tag :div, class: "alert #{name}" do
        flash_message_output msg
      end
    end.join.html_safe
  end

  def flash_message_output(content)
    if content.is_a? Array
      flash_list content
    else
      content
    end
  end

  def flash_list(messages)
    content_tag :ul do
      messages.map do |message|
        content_tag(:li, message)
      end.join.html_safe
    end
  end
end

Edit application.html.erb as follows:

<!DOCTYPE html>
<html>
<head>
  <title>Nevernote</title>
  <%= stylesheet_link_tag    'application', media: 'all' %>
  <%= javascript_include_tag 'application' %>
  <%= csrf_meta_tags %>
</head>
<body>

<div class="container wrap">
  <div class="row">
    <div class="col-xs-12">
      <header>
        <div class="well">Nevernote</div>
      </header>
    </div>
  </header>
  <div class="container">
    <div class="row">
      <div class="col-xs-4">
        <nav id="sidebar">
          <div class="well">
            SIDEBAR
          </div>
        </nav>
      </div>
      <div class="col-xs-8">
        <main>
          <div class="well">
            <!-- BEGIN main content -->
            <%= flash_messages(flash) %>
            <%= yield %>
          </div>
        </main>
      </div>
    </div>
  </div>
</div>

</body>
</html>

The same thing in HAML:

!!!
%html
  %head
    %title Elevennote
    = stylesheet_link_tag    'application', media: 'all'
    = javascript_include_tag 'application'
    = csrf_meta_tags
  %body
    .container.wrap
      .row
        .col-xs-12
          %header
            .well
              ElevenNote
              = link_to 'Sign Up', sign_up_path
        .container
          .row
            .col-xs-4
              %nav#sidebar
                .well
                  SIDEBAR
            .col-xs-8
              %main
                .well
                  = flash_messages(flash)
                  = yield

Create a new stylesheet named style.css.scss:

$highlight_color: #2dbe60;

body {
  font-family: Oxygen, Helvetica, Arial, sans-serif;
}
.container {
  width: 100% !important;
  padding-left: 0 !important;
  padding-right: 0 !important;
}

.row {
  margin-left: 0 !important;
  margin-right: 0 !important;
}

.col-xs-12 {
  padding-left: 0 !important;
  padding-right: 0 !important;
}

header .well {
  padding-top: 5px;
  padding-bottom: 5px;
  .user-links {
    float: right;
    color: #ccc;
  }
}
h1, h2, h3, h4, h5 {
  font-family: Merriweather;
  font-weight: 300;
  color: $highlight_color;
}
a:link, a:visited, a:hover, a:active {
  color: $highlight_color;
}
.bootsy_text_area {
  width: 100%;
  border-color: #ccc;
  border: none;
  font-size: 120%;
  &:focus {
    outline: none;
  }
}

i.fa-trash-o {
  color: #ccc;
  font-size: 150%;
  vertical-align: middle;
  margin-left: 1em;
  &:hover {
    color: #999;
  }
}
.wysihtml5-toolbar {
  .btn-default {
    color: #999;
  }
}
input#note_title {
  border: none;
  font-size: 200%;
  font-family: Merriweather;
  color: $highlight_color;
  font-weight: 300;
  width: 100%;
  &:focus {
    outline: none;
  }
}
ul#notes {
  list-style: none;
  margin-top: 1em;
  padding: 0;
  width: 100%;
  color: #999;
  li {
    border-top: 1px solid #ddd;
    padding: 1em;
    height: 100px;
    font-size: 90%;
    cursor: pointer;
    overflow: hidden;
    &:first-of-type {
      border-top: none;
    }
    &:hover {
      color: white;
      background-color: $highlight_color;
      .note-title {
        color: white;
      }
    }
    .note-title {
      color: black;
      font-family: Merriweather;
      font-size: 120%;
      font-weight: 300;
    }
    .note-body {
      height: 54px;
      overflow: hidden;
    }
  }
}
#new_user {
  input[type="text"],
  input[type="password"] {
    width: 300px;
    font-size: 120%;
    border-radius: 4px;
    border: 1px solid #ccc;
    padding: 0.25em;
  }
}
#welcome {
  h1 {
    color: $highlight_color
  }
  main {
    margin: 0 auto;
    width: 360px;
  }
}
span.login {
  margin-left: 1em;
}

Let's also add a default route so we have a page we can view in the browser to admire our new layout.

config/routes.rb

root 'welcome#index'

app/controllers/welcome_controller.rb

class WelcomeController < ApplicationController
end

app/views/welcome/index.html.erb

Welcome to Nevernote.