読者です 読者をやめる 読者になる 読者になる

ruby on rails 4でbootstrap3を適用

Gemfileに以下を追加

$ vi Gemfile

gem 'twitter-bootstrap3-rails'

$ bundle install

CSSとJSを追加する

$ rails generate bootstrap:install static

"Could not find a JavaScript runtime"のようなエラーが出る場合は以下のコマンドを実行

$ sudo apt-get install nodejs

レイアウトを作成(fluid->レスポンシブ対応)

$ rails g bootstrap:layout application fluid

グローバルメニューを追加、レスポンシブ

試しに静的ページを作って見てみる

$ rails generate controller DashBoard home

ブラウザでhttp://localhost:3000/dash_board/homeにアクセスするとレスポンシブページができていることが確認できる。試しにブラウザのサイズを小さくすると、ヘッダーが変化している。

ヘッダーをつける

$ vi app/views/layouts/application.html.erb

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><%= content_for?(:title) ? yield(:title) : "YokohamaApp" %></title>
    <%= csrf_meta_tags %>

  <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
  <!--[if lt IE 9]>
    <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.6.1/html5shiv.js" type="text/javascript"></script>
  <![endif]-->

  <%= stylesheet_link_tag "application", :media => "all" %>
  <!-- For third-generation iPad with high-resolution Retina display: -->
  <!-- Size should be 144 x 144 pixels -->
  <%= favicon_link_tag 'apple-touch-icon-144x144-precomposed.png', :rel => 'apple-touch-icon-precomposed', :type => 'image/png', :sizes => '144x144' %>

  <!-- For iPhone with high-resolution Retina display: -->
  <!-- Size should be 114 x 114 pixels -->
  <%= favicon_link_tag 'apple-touch-icon-114x114-precomposed.png', :rel => 'apple-touch-icon-precomposed', :type => 'image/png', :sizes => '114x114' %>

  <!-- For first- and second-generation iPad: -->
  <!-- Size should be 72 x 72 pixels -->
  <%= favicon_link_tag 'apple-touch-icon-72x72-precomposed.png', :rel => 'apple-touch-icon-precomposed', :type => 'image/png', :sizes => '72x72' %>

  <!-- For non-Retina iPhone, iPod Touch, and Android 2.1+ devices: -->
  <!-- Size should be 57 x 57 pixels -->
  <%= favicon_link_tag 'apple-touch-icon-precomposed.png', :rel => 'apple-touch-icon-precomposed', :type => 'image/png' %>

  <!-- For all other devices -->
  <!-- Size should be 32 x 32 pixels -->
  <%= favicon_link_tag 'favicon.ico', :rel => 'shortcut icon' %>
  <%= javascript_include_tag "application" %>
</head>
<body>

<nav class="navbar navbar-default" role="navigation">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <%= link_to "LifeLogApp", { :controller => :dash_board, :action => :home }, { :class => "navbar-brand"} %>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left" role="search">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li class="divider"></li>
            <li><a href="#">Separated link</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

  <div class="container">
    <div class="row">
      <div class="col-md-12">
        <%= bootstrap_flash %>
        <%= yield %>
      </div>
    </div><!--/row-->

    <footer>
      <p>&copy; LifeLogApp 2015</p>
    </footer>

  </div> <!-- /container -->

</body>

これでグローバルメニューが作成される