Tambourine作業メモ

主にスキル習得のためにやった作業のメモ。他人には基本的に無用のものです。

Rails チュートリアルをやってみる(8) 3.4〜3.5 ちょっと動的っぽいページ

3.4

理由はわからないけど、レイアウトファイルをリネームしておけと言われるので、する

> mv app/views/layouts/application.html.erb layout_file
> ls
Gemfile      README.md    app          config       db           lib          package.json test         vendor
Gemfile.lock Rakefile     bin          config.ru    layout_file  log          public       tmp

画面の内容をテストするらしい。それってコントローラーのテストなの?感はあるけど、そんなことはどうでもいいのだ。 こんな感じでtitleタグの中身がチェックできるらしい。

test/controllers/static_pages_controller_test.rb

  test "should get home" do
    get static_pages_home_url
    assert_response :success
    assert_select "title", "Home | Ruby on Rails Tutorial Sample App"
  end

helpもaboutも同じようにして、テスト。レッドになる。

> bundle exec rails t test/controllers/static_pages_controller_test.rb 
(例)

# Running:

FFF

Finished in 0.360505s, 8.3217 runs/s, 16.6433 assertions/s.

  1) Failure:
StaticPagesControllerTest#test_should_get_help [/Users/tambara/study/rails_study/sample_app/test/controllers/static_pages_controller_test.rb:13]:
Expected at least 1 element matching "title", found 0..
Expected 0 to be >= 1.

  2) Failure:
StaticPagesControllerTest#test_should_get_home [/Users/tambara/study/rails_study/sample_app/test/controllers/static_pages_controller_test.rb:7]:
Expected at least 1 element matching "title", found 0..
Expected 0 to be >= 1.

  3) Failure:
StaticPagesControllerTest#test_should_get_about [/Users/tambara/study/rails_study/sample_app/test/controllers/static_pages_controller_test.rb:19]:
Expected at least 1 element matching "title", found 0..
Expected 0 to be >= 1.

3 runs, 6 assertions, 3 failures, 0 errors, 0 skips

これを通すために、各ビューを修正するわけだ。まあ、そりゃ通るだろう。 今のビューは完全なHTMLになっていないので、<body>にパックされている。 これを完全なHTMLにする。

app/views/static_pages/about.html.erb

<!DOCTYPE html>
<html>

<head>
    <title>About | Ruby on Rails Tutorial Sample App</title>
</head>

<body>
    <h1>About</h1>
    <p>
        <a href="https://railstutorial.jp/">Ruby on Rails Tutorial</a>
        is a <a href="https://railstutorial.jp/#ebook">book</a> and
        <a href="https://railstutorial.jp/#screencast">screencast</a>
        to teach web development with
        <a href="http://rubyonrails.org/">Ruby on Rails</a>.
        This is the sample application for the tutorial.
    </p>
</body>

</html>

テストすると、ちゃんとレッド減ってる。

> bundle exec rails t test/controllers/static_pages_controller_test.rb 
(略)
# Running:

F.F

Finished in 0.347614s, 8.6303 runs/s, 17.2605 assertions/s.

  1) Failure:
StaticPagesControllerTest#test_should_get_home [/Users/tambara/study/rails_study/sample_app/test/controllers/static_pages_controller_test.rb:7]:
Expected at least 1 element matching "title", found 0..
Expected 0 to be >= 1.

  2) Failure:
StaticPagesControllerTest#test_should_get_help [/Users/tambara/study/rails_study/sample_app/test/controllers/static_pages_controller_test.rb:13]:
Expected at least 1 element matching "title", found 0..
Expected 0 to be >= 1.

3 runs, 6 assertions, 2 failures, 0 errors, 0 skips

演習は、テストにsetupを追加して重複を消すこと。これは特に疑問ないね。

3.4.3に進む。

リネームしていたレイアウトファイルを戻して、タイトルの動的生成を組み込む。

provideは各ページ別のテンプレートに入れて、それをyieldする方はレイアウトファイルに入れる。

演習で、Contactを追加する。修正はこんな感じでいいはず。

> git diff
diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb
index 19f79a9..d304760 100644
--- a/app/controllers/static_pages_controller.rb
+++ b/app/controllers/static_pages_controller.rb
@@ -7,4 +7,7 @@ class StaticPagesController < ApplicationController
 
   def about
   end
+
+  def contact
+  end
 end
diff --git a/config/routes.rb b/config/routes.rb
index a286a2a..74028be 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -2,6 +2,7 @@ Rails.application.routes.draw do
   get 'static_pages/home'
   get 'static_pages/help'
   get 'static_pages/about'
+  get 'static_pages/contact'
 
   root 'application#hello'
 end
diff --git a/test/controllers/static_pages_controller_test.rb b/test/controllers/static_pages_controller_test.rb
index 636f458..a0fc70b 100644
--- a/test/controllers/static_pages_controller_test.rb
+++ b/test/controllers/static_pages_controller_test.rb
@@ -23,4 +23,9 @@ class StaticPagesControllerTest < ActionDispatch::IntegrationTest
     assert_select "title", "About | #{@base_title}"
   end
 
+  test "should get contact" do
+    get static_pages_contact_url
+    assert_response :success
+    assert_select "title", "Contact | #{@base_title}"
+  end
 end
(END)

あ、あとapp/views/static_pages/contact.html.erbを作った。これ、git diffに出てこないのどうしたらいいのかわからない。

続いて、3.4.4。ここは最初にいい加減につくったルートページをやめて、 ここで作ったHomeのページにする。

> git diff config/routes.rb 
diff --git a/config/routes.rb b/config/routes.rb
index a286a2a..d3be50f 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,7 +1,7 @@
 Rails.application.routes.draw do
+  root 'static_pages#home'
   get 'static_pages/home'
   get 'static_pages/help'
   get 'static_pages/about'
-
-  root 'application#hello'
+  get 'static_pages/contact'
 end

演習はこれのテストの追加。

最後にコミットしたあと、masterブランチにマージして、Herokuにプッシュして終了。

> git checkout master
Switched to branch 'master'
> git branch
* master
  static-pages
> git merge static-pages 
Updating 9fcb567..abe4bbf
Fast-forward
 app/assets/javascripts/static_pages.coffee       |  3 +++
 app/assets/stylesheets/static_pages.scss         |  3 +++
 app/controllers/application_controller.rb        |  4 ----
 app/controllers/static_pages_controller.rb       | 13 +++++++++++++
 app/helpers/static_pages_helper.rb               |  2 ++
 app/views/layouts/application.html.erb           |  2 +-
 app/views/static_pages/about.html.erb            | 10 ++++++++++
 app/views/static_pages/contact.html.erb          |  6 ++++++
 app/views/static_pages/help.html.erb             |  9 +++++++++
 app/views/static_pages/home.html.erb             |  7 +++++++
 config/routes.rb                                 |  6 +++++-
 test/controllers/static_pages_controller_test.rb | 37 +++++++++++++++++++++++++++++++++++++
 12 files changed, 96 insertions(+), 6 deletions(-)
 create mode 100644 app/assets/javascripts/static_pages.coffee
 create mode 100644 app/assets/stylesheets/static_pages.scss
 create mode 100644 app/controllers/static_pages_controller.rb
 create mode 100644 app/helpers/static_pages_helper.rb
 create mode 100644 app/views/static_pages/about.html.erb
 create mode 100644 app/views/static_pages/contact.html.erb
 create mode 100644 app/views/static_pages/help.html.erb
 create mode 100644 app/views/static_pages/home.html.erb
 create mode 100644 test/controllers/static_pages_controller_test.rb
> git push heroku master
Counting objects: 68, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (67/67), done.
Writing objects: 100% (68/68), 6.03 KiB | 771.00 KiB/s, done.
Total 68 (delta 35), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Ruby app detected
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-2.4.5
remote: -----> Installing dependencies using bundler 1.15.2
remote:        Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
remote:        Warning: the running version of Bundler (1.15.2) is older than the version that created the lockfile (1.17.1). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`.
remote:        Fetching gem metadata from https://rubygems.org/.........
remote:        Fetching version metadata from https://rubygems.org/..
remote:        Fetching dependency metadata from https://rubygems.org/.
remote:        Using rake 12.3.1
remote:        Using concurrent-ruby 1.1.3
remote:        Using minitest 5.10.3
remote:        Using thread_safe 0.3.6
remote:        Using builder 3.2.3
remote:        Using erubi 1.7.1
remote:        Using mini_portile2 2.3.0
remote:        Using crass 1.0.4
remote:        Using rack 2.0.6
remote:        Using nio4r 2.3.1
remote:        Using websocket-extensions 0.1.3
remote:        Using mini_mime 1.0.1
remote:        Using arel 8.0.0
remote:        Using bundler 1.15.2
remote:        Using coffee-script-source 1.12.2
remote:        Using execjs 2.7.0
remote:        Using method_source 0.9.2
remote:        Using thor 0.20.3
remote:        Using ffi 1.9.25
remote:        Using multi_json 1.13.1
remote:        Using pg 0.20.0
remote:        Using puma 3.9.1
remote:        Using rb-fsevent 0.10.3
remote:        Using tilt 2.0.8
remote:        Using turbolinks-source 5.2.0
remote:        Using i18n 1.1.1
remote:        Using nokogiri 1.8.5
remote:        Using tzinfo 1.2.5
remote:        Using rack-test 1.1.0
remote:        Using sprockets 3.7.2
remote:        Using mail 2.7.1
remote:        Using websocket-driver 0.6.5
remote:        Using uglifier 3.2.0
remote:        Using coffee-script 2.4.1
remote:        Using rb-inotify 0.9.10
remote:        Using turbolinks 5.0.1
remote:        Using loofah 2.2.3
remote:        Using activesupport 5.1.6
remote:        Using rails-dom-testing 2.0.3
remote:        Using globalid 0.4.1
remote:        Using activemodel 5.1.6
remote:        Using jbuilder 2.7.0
remote:        Using rails-html-sanitizer 1.0.4
remote:        Using sass-listen 4.0.0
remote:        Using activejob 5.1.6
remote:        Using activerecord 5.1.6
remote:        Using actionview 5.1.6
remote:        Using sass 3.7.2
remote:        Using actionpack 5.1.6
remote:        Using actioncable 5.1.6
remote:        Using actionmailer 5.1.6
remote:        Using railties 5.1.6
remote:        Using sprockets-rails 3.2.1
remote:        Using coffee-rails 4.2.2
remote:        Using jquery-rails 4.3.1
remote:        Using rails 5.1.6
remote:        Using sass-rails 5.0.6
remote:        Bundle complete! 20 Gemfile dependencies, 57 gems now installed.
remote:        Gems in the groups development and test were not installed.
remote:        Bundled gems are installed into ./vendor/bundle.
remote:        Bundle completed (3.32s)
remote:        Cleaning up the bundler cache.
remote:        Warning: the running version of Bundler (1.15.2) is older than the version that created the lockfile (1.17.1). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`.
remote:        The latest bundler is 2.0.0.pre.1, but you are currently running 1.15.2.
remote:        To update, run `gem install bundler --pre`
remote: -----> Installing node-v8.10.0-linux-x64
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote:        Running: rake assets:precompile
remote:        Yarn executable was not detected in the system.
remote:        Download Yarn at https://yarnpkg.com/en/docs/install
remote:        I, [2018-11-22T15:04:09.227093 #396]  INFO -- : Writing /tmp/build_71b6afdcc7eaadb5213bbf0960c620b0/public/assets/application-86a3fd3b1e217ceb8f2069261313e734793e5074b3f462c54a813438e80e7839.js
remote:        I, [2018-11-22T15:04:09.227761 #396]  INFO -- : Writing /tmp/build_71b6afdcc7eaadb5213bbf0960c620b0/public/assets/application-86a3fd3b1e217ceb8f2069261313e734793e5074b3f462c54a813438e80e7839.js.gz
remote:        Asset precompilation completed (3.03s)
remote:        Cleaning assets
remote:        Running: rake assets:clean
remote: -----> Detecting rails configuration
remote: 
remote: ###### WARNING:
remote: 
remote:        You have not declared a Ruby version in your Gemfile.
remote:        To set your Ruby version add this line to your Gemfile:
remote:        ruby '2.4.5'
remote:        # See https://devcenter.heroku.com/articles/ruby-versions for more information.
remote: 
remote: ###### WARNING:
remote: 
remote:        No Procfile detected, using the default web server.
remote:        We recommend explicitly declaring how to boot your server process via a Procfile.
remote:        https://devcenter.heroku.com/articles/ruby-default-web-server
remote: 
remote: 
remote: -----> Discovering process types
remote:        Procfile declares types     -> (none)
remote:        Default types for buildpack -> console, rake, web
remote: 
remote: -----> Compressing...
remote:        Done: 41M
remote: -----> Launching...
remote:        Released v7
remote:        https://salty-eyrie-75181.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/salty-eyrie-75181.git
   9fcb567..abe4bbf  master -> master