LoginSignup
0
1

More than 1 year has passed since last update.

railsチュートリアル第五章 ルートurl

Posted at

RailsのルートURL

名前付きルートをサンプルアプリケーションの静的ページで使うために、ルーティング用のファイル(config/routes.rb)を編集していきます
静的ページについても同様にルーティングを設定

私たちはこれまでに、ルートURLを定義するコードを3回見てきました。
Helloアプリケーションのコード

root 'application#hello'

Toyアプリケーションのコード

root 'users#index'

Sampleアプリケーションのコード

root 'static_pages#home'

いずれの場合においても、rootメソッドを使ってルートURL "/" をコントローラーのアクションに紐付けていました。
ルートURLのようなルーティングを定義することの効果は、ブラウザからアクセスしやすくすることだけではありません
それ以外にも、生のURLではなく、名前付きルートを使ってURLを参照することができるようになります。
例えばルートURLを定義すると、root_pathやroot_urlといったメソッドを通してURLを参照することができます
ちなみに前者はルートURL以下の文字列を、後者は完全なURLの文字列を返します。

root_path -> '/'
root_url  -> 'https://www.example.com/'

Railsチュートリアルでは一般的な規約に従い、基本的には_path書式を使い、リダイレクトの場合のみ_url書式を使うようにします
HTTPの標準としては、リダイレクトのときに完全なURLが要求されるためです。ただしほとんどのブラウザでは、どちらの方法でも動作します。

HelpページやAboutページ、Contactページなどの名前付きルートを定義していきましょう
getルールを使って定義していきます

get 'static_pages/help'

変換

get  '/help', to: 'static_pages#help'

getルールを使って変更すると、GETリクエストが /help に送信されたときにStaticPagesコントローラーのhelpアクションを呼び出してくれるようになります
ルートURLのときと同様に、help_pathやhelp_urlといった名前付きルートも使えるようになります

help_path -> '/help'
help_url  -> 'https://www.example.com/help'

他の静的ページについても同様にルーティングを変更していくと、リスト 5.23はリスト 5.27のようなコードになります。

静的なページのルーティング一覧
Rails.application.routes.draw do
  root 'static_pages#home'
  get  '/help',    to: 'static_pages#help'
  get  '/about',   to: 'static_pages#about'
  get  '/contact', to: 'static_pages#contact'
end

今後は常にroot_pathまたはroot_urlを使っていきます
ルーティングの変更で名前付きルート(*_path)が使えるようになったので、早速テストの修正で使っている点に注意

StaticPagesで扱う新しい名前付きルートに対するテスト
require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest

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

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

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

  test "should get contact" do
    get contact_path
    assert_response :success
    assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
  end
end

演習

1.Helpページの名前付きルートをhelfに変更してみてください
asで名前付きルートを変えられるらしい。

Rails.application.routes.draw do
  root 'static_pages#home'
  get  '/help',    to: 'static_pages#help', as: 'helf'
  get  '/about',   to: 'static_pages#about'
  get  '/contact', to: 'static_pages#contact'
end
ubuntu:~/environment/sample_app (filling-in-layout) $ rails t
Running via Spring preloader in process 12888
Started with run options --seed 3286

ERROR["test_should_get_help", #<Minitest::Reporters::Suite:0x00005637d6d6f2b8 @name="StaticPagesControllerTest">, 2.9386599879999267]
 test_should_get_help#StaticPagesControllerTest (2.94s)
NameError:         NameError: undefined local variable or method `help_path' for #<StaticPagesControllerTest:0x00005637d6d66028>
            test/controllers/static_pages_controller_test.rb:32:in `block in <class:StaticPagesControllerTest>'
  5/5: [==============================] 100% Time: 00:00:03, Time: 00:00:03
Finished in 3.05525s
5 tests, 7 assertions, 0 failures, 1 errors, 0 skips

NameError: undefined local variable or method `help_path'
help_pathが見つからないというエラーらしい。

2.先ほどの変更により、テストが red になっていることを確認してください。リスト 5.28を参考にルーティングを更新して、テストを green にして見てください。

static_pages_controller_test.rb
require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest

.
.
.
 test "should get help" do
    get helf_path
    assert_response :success
    assert_select "title", "Help | #{@base_title}"
  end
.
.
.
ubuntu:~/environment/sample_app (filling-in-layout) $ rails t
Running via Spring preloader in process 13127
Started with run options --seed 56818

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

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

理由はわからない。
しかしas helfと変更したのでhelf_pathと書き直すとテストは成功した。

3.エディタのUndo機能を使って、今回の演習で行った変更を元に戻して見てください。
元に戻した。

0
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
0
1