Rails Tutorial Chapter 5 ling in the layout
Chapter 5/ Filling in the layout | Ruby on Rails Tutorial (Rails 5)
レイアウトの作成
この章では、アプリケーションにBootstrapフレームワークを組み込み、そして、カスタムスタイルを追加します1。また、これまで作成したページ (HomeやAboutなど) へのリンクをレイアウトに追加します
ということでアプリケーションへのレイアウトの追加や修正について学ぶ。
構造を追加する
app/views/layouts/application.html.erb
を以下のように書き換える。
<!DOCTYPE html> <html> <head> <title><%= full_title(yield(:title)) %></title> <%= csrf_meta_tags %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> <!--[if lt IE 9]> <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/r29/html5.min.js"> </script> <![endif]--> </head> <body> <header class="navbar navbar-fixed-top navbar-inverse"> <div class="container"> <%= link_to "sample app", '#', id: "logo" %> <nav> <ul class="nav navbar-nav navbar-right"> <li><%= link_to "Home", '#' %></li> <li><%= link_to "Help", '#' %></li> <li><%= link_to "Log in", '#' %></li> </ul> </nav> </div> </header> <div class="container"> <%= yield %> </div> </body> </html>
"HTML5 shim" についての説明に始まり、構成している部品についての説明が続く。
Bootstrap なしで、ひとまずナビゲーションやロゴが表示される状態になる。
BootstrapとカスタムCSS
Gemfile
に以下を追記する。
source 'https://rubygems.org' gem 'rails', '5.0.0.1' gem 'bootstrap-sass', '3.3.6' . . .
Rails tutorial 4.2 では '3.2.0.0
を使用していたようだが、Rails tutorial 5 では 3.3.6
になっているのでそちらに合わせておく。
Gemfile を書き換えたら以下を実行する。
$ bundle install Fetching gem metadata from https://rubygems.org/ Fetching version metadata from https://rubygems.org/ Fetching dependency metadata from https://rubygems.org/ Resolving dependencies... Using rake 11.2.2 Using concurrent-ruby 1.0.2 Using i18n 0.7.0 Using minitest 5.9.0 Using thread_safe 0.3.5 Using builder 3.2.2 Using erubis 2.7.0 Using mini_portile2 2.1.0 Using pkg-config 1.1.7 Using rack 2.0.1 Using nio4r 1.2.1 Using websocket-extensions 0.1.2 Using mime-types-data 3.2016.0521 Using arel 7.1.1 Using ansi 1.5.0 Using execjs 2.7.0 Using sass 3.4.22 Using bundler 1.12.5 Using byebug 9.0.0 Using coderay 1.1.1 Using coffee-script-source 1.10.0 Using method_source 0.8.2 Using thor 0.19.1 Using debug_inspector 0.0.2 Using ffi 1.9.14 Using formatador 0.2.5 Using rb-fsevent 0.9.7 Using lumberjack 1.0.10 Using nenv 0.3.0 Using shellany 0.0.1 Using slop 3.6.0 Using guard-compat 1.2.1 Using multi_json 1.12.1 Using ruby-progressbar 1.8.1 Using puma 3.4.0 Using tilt 2.0.5 Using spring 1.7.2 Using sqlite3 1.3.11 Using turbolinks-source 5.0.0 Using tzinfo 1.2.2 Using nokogiri 1.6.8 Using rack-test 0.6.3 Using sprockets 3.7.0 Using websocket-driver 0.6.4 Using mime-types 3.1 Installing autoprefixer-rails 6.5.0.1 Using uglifier 3.0.0 Using coffee-script 2.4.1 Using rb-inotify 0.9.7 Using notiffany 0.1.1 Using pry 0.10.4 Using guard-minitest 2.4.4 Using minitest-reporters 1.1.9 Using turbolinks 5.0.1 Using activesupport 5.0.0.1 Using loofah 2.0.3 Using mail 2.6.4 Installing bootstrap-sass 3.3.6 Using listen 3.0.8 Using rails-dom-testing 2.0.1 Using globalid 0.3.7 Using activemodel 5.0.0.1 Using jbuilder 2.4.1 Using rails-html-sanitizer 1.0.3 Using guard 2.13.0 Using spring-watcher-listen 2.0.0 Using activejob 5.0.0.1 Using activerecord 5.0.0.1 Using actionview 5.0.0.1 Using actionpack 5.0.0.1 Using actioncable 5.0.0.1 Using actionmailer 5.0.0.1 Using railties 5.0.0.1 Using sprockets-rails 3.2.0 Using rails-controller-testing 0.1.1 Using coffee-rails 4.2.1 Using jquery-rails 4.1.1 Using web-console 3.1.1 Using rails 5.0.0.1 Using sass-rails 5.0.6 Bundle complete! 21 Gemfile dependencies, 80 gems now installed. Gems in the group production were not installed. Use `bundle show [gemname]` to see where a bundled gem is installed.
成功。
続いてカスタムCSSファイルを作成する。
まずはからファイルの作成。
$ touch app/assets/stylesheets/custom.scss
app/assets/stylesheets/
はAsset Pipeline の一部。このディレクトリに置かれたスタイルシートはapplication.cssの一部として自動的にWebサイトのレイアウトにインクルードされる。
@import "bootstrap-sprockets"; @import "bootstrap"; /* universal */ body { padding-top: 60px; } section { overflow: auto; } textarea { resize: vertical; } .center { text-align: center; } .center h1 { margin-bottom: 10px; } /* typography */ h1, h2, h3, h4, h5, h6 { line-height: 1; } h1 { font-size: 3em; letter-spacing: -2px; margin-bottom: 30px; text-align: center; } h2 { font-size: 1.2em; letter-spacing: -1px; margin-bottom: 30px; text-align: center; font-weight: normal; color: #777; } p { font-size: 1.1em; line-height: 1.7em; } /* header */ #logo { float: left; margin-right: 10px; font-size: 1.7em; color: #fff; text-transform: uppercase; letter-spacing: -1px; padding-top: 9px; font-weight: bold; } #logo:hover { color: #fff; text-decoration: none; } /* footer */ footer { margin-top: 45px; padding-top: 5px; border-top: 1px solid #eaeaea; color: #777; } footer a { color: #555; } footer a:hover { color: #222; } footer small { float: left; } footer ul { float: right; list-style: none; } footer ul li { float: left; margin-left: 15px; }
これで Web サーバを再度スタートさせると、Bootstrap が適用されている。色使いやボタンの見栄えなどがよい感じになっている。
パーシャル
HTML の中から HTML shim を隠したり、HTMLヘッダーに関する記述を一箇所にまとめたりと言った機能が Rails では、パーシャルという名で提供されている。
app/views/layouts/application.html.erb
を下記のように書き換える。
<!DOCTYPE html> <html> <head> <title><%= full_title(yield(:title)) %></title> <%= csrf_meta_tags %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> <%= render 'layouts/shim' %> </head> <body> <%= render 'layouts/header' %> <div class="container"> <%= yield %> <%= render 'layouts/footer' %> </div> </body> </html>
app/views/layouts/_shim.html.erb
を作成し、以下を記載。
<!--[if lt IE 9]> <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/r29/html5.min.js"> </script> <![endif]-->
app/views/layouts/_header.html.erb
を作成し、以下を記載。
<header class="navbar navbar-fixed-top navbar-inverse"> <div class="container"> <%= link_to "sample app", '#', id: "logo" %> <nav> <ul class="nav navbar-nav navbar-right"> <li><%= link_to "Home", '#' %></li> <li><%= link_to "Help", '#' %></li> <li><%= link_to "Log in", '#' %></li> </ul> </nav> </div> </header>
app/views/layouts/_footer.html.erb
も下記のように。
<footer class="footer"> <small> The <a href="http://www.railstutorial.org/">Ruby on Rails Tutorial</a> by <a href="http://www.michaelhartl.com/">Michael Hartl</a> </small> <nav> <ul> <li><%= link_to "About", '#' %></li> <li><%= link_to "Contact", '#' %></li> <li><a href="http://news.railstutorial.org/">News</a></li> </ul> </nav> </footer>
新たにフッターも追加され、rails server
するとよい感じになっているのがわかる。