6:少しだけ動的なページ

ページの内容に応じて、ページのタイトルを自ら書き換えて表示するようにします。レイアウトもデフォルトで作成されますがコピーを取っておきます
$ mv app/views/layouts/application.html.erb layout_file
StaticPagesコントローラのタイトルをテストするが、コントローラーを変更する
$ gedit 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” ←追記
end
  test “should get help” do
    get static_pages_help_url
    assert_response :success
    assert_select “title”, “Help | Ruby on Rails Tutorial Sample App” ←追記
end

 test “should get about” do
    get static_pages_about_url
    assert_response :success
    assert_select “title”, “About | Ruby on Rails Tutorial Sample App” ←追記   end
end
テストする
$ rails test
前略
3) Failure: StaticPagesControllerTest#test_should_get_help [/Rails/environment/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, 3 failures, 0 errors, 0 skips
3つfaileresが出ている。エラーに’Expected at least 1 element matching “title”, found 0’と出ている。viewにtitleが無いと言っているので
$ gedit 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とaboutも作る 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>
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=”https://rubyonrails.org/”>Ruby on Rails</a>.
      This is the sample app for the tutorial.
    </p>
  </body>
</html>
本来のhtmlは上の例の様に<!DOCTYPE html>が入り<head>と<body>タグが入る。でテスト
$ rails test
前略
3 runs, 6 assertions, 0 failures, 0 errors, 0 skips
これでエラーは出なくなった。 現在の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”
  end

 

  test “should get help” do
    get static_pages_help_url
    assert_response :success
    assert_select “title”, “Help | Ruby on Rails Tutorial Sample App”
  end

  test “should get about” do
    get static_pages_about_url
    assert_response :success
    assert_select “title”, “About | Ruby on Rails Tutorial Sample App”
  end
end
ハイライトしている部分が同じような繰り返しになっているので、setupと言う特別なメソッドを使い改良
$ gedit test/controllers/static_pages_controller_test.rb
require “test_helper”
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
  
def setup 
          ←ここから
    @base_title = “Ruby on Rails Tutorial Sample App”
  end
              ←ここまで追記
  test “should get home” do
   
get static_pages_home_url
    assert_response :success
   
assert_select “title”, “Home | #{@base_title}”  ←変更
  end
  test “should get help” do
    get static_pages_help_url
    assert_response :success
   
assert_select “title”, “Help | #{@base_title}”  ←変更
  end
  test “should get about” do
    get static_pages_about_url
   
assert_response :success
    assert_select “title”, “About | #{@base_title}”
  ←変更
  end
end
まだ重複しているが、完全に同じでは内が、Railsのprovideメソッドを使ってタイトルをページごとに変更し、なるべく同じようにします。
$ gedit app/views/static_pages/home.html.erb
<% provide(:title, “Home”) %>  ←追記
<!DOCTYPE html>
<html>
  <head>
    <title><%= yield(:title) %> | 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>

テストする
$ rails test
前略
3 runs, 6 assertions, 0 failures, 0 errors, 0 skips
エラーは出ていない。HelpページとAboutページも同様に変更します
$ gedit app/views/static_pages/help.html.erb
<% provide(:title, “Help”) %>   ←追記
<!DOCTYPE html>
<html>
  <head>
    <title><%= yield(:title) %> | 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>
$ gedit app/views/static_pages/about.html.erb
<% provide(:title, “About”) %>   ←追記
<!DOCTYPE html>
<html>
  <head>
    <title><%= yield(:title) %> | 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=”https://rubyonrails.org/”>Ruby on Rails</a>.
      This is the sample app for the tutorial.
    </p>
  </body>
</html>
変更していたレイアウトファイルを次のコマンドでファイル名を元に戻す
$ mv layout_file app/views/layouts/application.html.erb
レイアウトファイルを変更する
$ gedit app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
  <head>
    <title><%= yield(:title) %> | Ruby on Rails Tutorial Sample App</title>  ←変更
    <meta name=”viewport” content=”width=device-width,initial-scale=1″>  ←変更
    <meta charset=”utf-8″>  ←変更
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>
    <%= stylesheet_link_tag “application”, “data-turbo-track”: “reload” %>
  </head>
  <body>
    <%= yield %>
  </body>
</html>
ここに出てきた、<%= yield %>、<%= csrf_meta_tags %>、<%= csp_meta_tag %>、<%= stylesheet_link_tag “application”, “data-turbo-track”: “reload” %> 等はチュートリアルを見て理解してください。
そしてテスト
$ rails test
前略
3 runs, 6 assertions, 0 failures, 0 errors, 0 skips

localhost:3000にアクセスしたらhoeを表示する様にする
$ gedit config/routes.rb
Rails.application.routes.draw do
  root “static_pages#home”   ←追記
  get ‘static_pages/home’
  get ‘static_pages/help’
  get “static_pages/about”
  get “static_pages/contact”
  root “application#hello”   ←削除する
end
ブラウザでhttp://localhost:3000/にアクセスする
 
getに登録
$ git add -A
$ git commit -m “Latest”

minitest reportersを使うとテスト結果の出力を見やすくフォーマットしてくれます。そのため
$ gedit test/test_helper.rb
ENV[“RAILS_ENV”] ||= “test”
require_relative “../config/environment”
require “rails/test_help”
require “minitest/reporters”   ←追記
Minitest::Reporters.use!       ←追記

class ActiveSupport::TestCase
  # Run tests in parallel with specified workers
  parallelize(workers: :number_of_processors)

  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  # Add more helper methods to be used by all tests here…
end
$ rails test
Running 5 tests in a single process (parallelization threshold is 50)
Started with run options –seed 58624

5/5: [===================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.27512s
5 tests, 9 assertions, 0 failures, 0 errors, 0 skips

チューとリルではGuardによるテストの自動化を行って降りますが、私は飛ばしました(設定が良くわからないので、後日勉強とします)

 

フジマル について

1947年生れ、東京電機大学二部電気通信工学科卒、最後はスリランカ航空で営業だったのですが2018年に㈱インパック・ジャパンに再就職。趣味:登山、スキー、車いじり、コンピューター
カテゴリー: Ruby on railsに挑戦(almalinux9) パーマリンク

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA


このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください