ほぼ静的なページの作成(その3)

開発スキルのないSIerからソフトウェアスキルを取得するため、WEDでモノづくりを計画しています。詳しい経緯はここ
※現在はruby on railsチュートリアル(ここから)を進めていってます。

前回の続きruby on rails チュートリアルの第3章に入っています。

今回は少しだけ動的なページを作って第3章を終わりにしようと思います。
Homeページ、Helpページ、Aboutページをそれぞれ編集し、最終的にページごとに異なるタイトルを表示するようにします。

前回のrails newコマンドでレイアウトがデフォルトで作成されます。ここでは学習のため、一時的に次のようにファイル名を変更していく感じです。

mv app/views/layouts/application.html.erb layout_file
タイトルをテストする (Red)

static_pages_controller_test.rbにassert_selectメソッドを追加する
assert_selectメソッドは特定のHTMLタグが存在するかどうかをテストできるとのこと

$ cat test/controllers/static_pages_controller_test.rb 
require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest
  test "should get home" do
    get static_pages_home_url
    assert_response :success
    assert_select "title", "Home | Ruby on Rails Tutorial Sample App" #ponta add
  end

  test "should get help" do
    get static_pages_help_url
    assert_response :success
    assert_select "title", "Help | Ruby on Rails Tutorial Sample App" #ponta add
  end

  test "should get about" do
    get static_pages_about_url
    assert_response :success
    assert_select "title", "About | Ruby on Rails Tutorial Sample App" #ponta add
  end
end

そしてtestします
※各ページにタイトルがないからfalseになります。

$ rails test
:
:
3 runs, 6 assertions, 3 failures, 0 errors, 0 skips
タイトルを追加する (Green)

各ページにタイトルを追加してテストがパスするようにします。
最初にhome

cat app/views/static_pages/home.html.erb 
<!DOCTYPE html>
<html>
  <head>
    <title>Home | Ruby on Rails Tutorial Sample App</title>
  </head>
  <body>
    <h1>Sample App</h1>
    <p>
      This is the home page for the
      <a href="https://railstutorial.jp/">Ruby on Rails Tutorial</a>
      sample application.
    </p>
  </body>
</html>

次はhelp

$ cat app/views/static_pages/help.html.erb 
<!DOCTYPE html>
<html>
  <head>
    <title>Help | Ruby on Rails Tutorial Sample App</title>
  </head>
  <body>
    <h1>Help</h1>
    <p>  Get help on the Ruby on Rails Tutorial at the
      <a href="https://railstutorial.jp/help">Rails Tutorial help
      page</a>.
      To get help on this sample app, see the
      <a href="https://railstutorial.jp/#ebook">
      <em>Ruby on Rails Tutorial</em> book</a>.
    </p>
  </body>
</html>

最後にabout

$ cat 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>

これでOKになるはずなので、テストしてみる。

$ rails test
:
3 runs, 6 assertions, 0 failures, 0 errors, 0 skips

OKになりました。

まだ全然動的ではないですが、次回が少しややこしい話になるので一旦今回はここまでにします。 少し動的なページになります! では^^