1
1

More than 3 years have passed since last update.

railsチュートリアル第3章 root_url

Posted at

ルーティングの設定

アプリケーションルートのルーティングを設定
rootでホーム画面に指定できるらしい。

Rails.application.routes.draw do
  root 'static_pages#home'
  # 何の意味がわからん
  # ホーム画面がhomeページなるらしい
  get 'static_pages/home'
  # static_pagesコントローラからhomeアクションに紐付けされる
  # getでアクセルすることでページを取得することができる
  get 'static_pages/help'
  # 何を書いているかはわからない
  get 'static_pages/about'
  # aboutアクションにGETリクエストを送る
  get 'static_pages/contact'
  # rootは流れだったような気がする
  # applicationコントローラのhelloアクションを起こす
end
ubuntu:~/environment/sample_app (static-pages) $ rails t
Running via Spring preloader in process 9166
Run options: --seed 20010

# Running:

....

Finished in 2.188092s, 1.8281 runs/s, 3.6562 assertions/s.
4 runs, 8 assertions, 0 failures, 0 errors, 0 skips

プレビューでも成功を確認
ホーム画面がhomeページになるらしい。
helpページに変えてみるとこれもホーム画面になるのか?試してみた。

Rails.application.routes.draw do
  root 'static_pages#help'
  # 何の意味がわからん
  # ホーム画面がhomeページなるらしい
  get 'static_pages/home'
  # static_pagesコントローラからhomeアクションに紐付けされる
  # getでアクセルすることでページを取得することができる
  get 'static_pages/help'
  # 何を書いているかはわからない
  get 'static_pages/about'
  # aboutアクションにGETリクエストを送る
  get 'static_pages/contact'
  # rootは流れだったような気がする
  # applicationコントローラのhelloアクションを起こす
end

プレビューでも成功を確認
推察は正しかった。 よかった。

演習

1.FILL_INと記された部分を置き換えてstatic_pages_home_urlが使えるようにしろ。

require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest

  def setup
  # メソッド名
    @base_title = "Ruby on Rails Tutorial Sample App"
    # インスタンス変数
  end

  test "should get root" do
    get root_url
    assert_response :success
  end

  test "should get home" do
    get static_pages_home_url
    # getはGETリクエストで受け付ける普通のWebページを示す
    # GETを受け付けるよね?
    assert_response :success
    # response:successはリクエストが成功し、レスポンスとともに要求に応じた情報が返される
    # GETリクエストが成功、ページが表示されたらいい。
    # response:successは、実際にはHTTPのステータスコード200 OKを指す
    assert_select "title", "Home | #{@base_title}"
    # assert_selectメソッドは、特定のHTMLタグが存在する確認
    # @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
    # aboutページを表示させる(GETリクエスト)
    assert_response :success
    # GETリクエストが成功したかを確認
    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

2.root.rbのroot 'static_pages#home'が影響しているのか?
試しにコメントにしてみる。

ubuntu:~/environment/sample_app (static-pages) $ rails t
Running via Spring preloader in process 3370
Run options: --seed 45079
# Running:

.E

Error:
StaticPagesControllerTest#test_should_get_root:
NameError: undefined local variable or method `root_url' for #<StaticPagesControllerTest:0x0000558c33f66070>
    test/controllers/static_pages_controller_test.rb:12:in `block in <class:StaticPagesControllerTest>'


rails test test/controllers/static_pages_controller_test.rb:11

...

Finished in 1.313945s, 3.8053 runs/s, 6.0885 assertions/s.
5 runs, 8 assertions, 0 failures, 1 errors, 0 skips

undefined local variable or method `root_url'
root_urlが見つからないらしい。
またコメントを消してみる。

runs, 8 assertions, 0 failures, 1 errors, 0 skips
ubuntu:~/environment/sample_app (static-pages) $ rails t
Running via Spring preloader in process 3650
Run options: --seed 13270

# Running:

.....

Finished in 1.180516s, 4.2354 runs/s, 7.6238 assertions/s.
5 runs, 9 assertions, 0 failures, 0 errors, 0 skips

成功。
root 'static_pages#home'
これを有効にするとroot_urlが存在するらしい。
つまりroot 'static_pages#home'がroot_urlらしい。

最後に

結局この章で何をやったのかわからなくてもいいらしい。

高度なセットアップ

テスト用設定について解説。
成功/失敗の表示設定をする「minitest reporters(3.6.1)」と、
ファイルの変更を検出して必要なテストだけを自動実行してくれる「Guard(3.6.2)」
の2つです。

minitest reporters

ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!

class ActiveSupport::TestCase
  # 特定のワーカーではテストをパラレル実行する
  parallelize(workers: :number_of_processors)

  # すべてのテストがアルファベット順に実行されるよう、
  #test/fixtures/*.ymlにあるすべてのfixtureをセットアップする
  fixtures :all

  # (すべてのテストで使うその他のヘルパーメソッドは省略)
end

テストの結果表示が変わる。

ubuntu:~/environment/sample_app (master) $ rails t
Running via Spring preloader in process 9970
Started with run options --seed 46150

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

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

Guardによるテストの自動化

Guardを使ってテストを自動的に実行させるようにしてみましょう。
home.html.erbファイルが変更されたら
static_pages_controller_test.rbを自動的に実行することを
Guardで設定することができます。

# Guardのマッチング規則を定義
guard :minitest, spring: "bin/rails test", all_on_start: false do
  watch(%r{^test/(.*)/?(.*)_test\.rb$})
  watch('test/test_helper.rb') { 'test' }
  watch('config/routes.rb') { interface_tests }
  watch(%r{app/views/layouts/*}) { interface_tests }
  watch(%r{^app/models/(.*?)\.rb$}) do |matches|
    "test/models/#{matches[1]}_test.rb"
  end
  watch(%r{^app/controllers/(.*?)_controller\.rb$}) do |matches|
    resource_tests(matches[1])
  end
  watch(%r{^app/views/([^/]*?)/.*\.html\.erb$}) do |matches|
    ["test/controllers/#{matches[1]}_controller_test.rb"] +
    integration_tests(matches[1])
  end
  watch(%r{^app/helpers/(.*?)_helper\.rb$}) do |matches|
    integration_tests(matches[1])
  end
  watch('app/views/layouts/application.html.erb') do
    'test/integration/site_layout_test.rb'
  end
  watch('app/helpers/sessions_helper.rb') do
    integration_tests << 'test/helpers/sessions_helper_test.rb'
  end
  watch('app/controllers/sessions_controller.rb') do
    ['test/controllers/sessions_controller_test.rb',
     'test/integration/users_login_test.rb']
  end
  watch('app/controllers/account_activations_controller.rb') do
    'test/integration/users_signup_test.rb'
  end
  watch(%r{app/views/users/*}) do
    resource_tests('users') +
    ['test/integration/microposts_interface_test.rb']
  end
end

# 与えられたリソースに対応する統合テストを返す
def integration_tests(resource = :all)
  if resource == :all
    Dir["test/integration/*"]
  else
    Dir["test/integration/#{resource}_*.rb"]
  end
end

# インターフェースが該当するすべてのテストを返す
def interface_tests
  integration_tests << "test/controllers/"
end

# 与えられたリソースに対応するコントローラのテストを返す
def controller_test(resource)
  "test/controllers/#{resource}_controller_test.rb"
end

# 与えられたリソースに対応するすべてのテストを返す
def resource_tests(resource)
  integration_tests(resource) << controller_test(resource)
end

何しているかわからん。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1