0
0

More than 1 year has passed since last update.

railsチュートリアル第五章 ユーザ登録用url

Posted at

ユーザー登録用url

新規ユーザー用の動作するページが/users/newにできました。
URLは/users/newではなく表のとおりに/signupにしたいと思います
ユーザー登録URL用にget '/signup'のルートを追加します

ユーザー登録ページのルート red

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

ボタンにユーザー登録ページへのリンクを追加する

<div class="center jumbotron">
  <h1>Welcome to the Sample App</h1>

  <h2>
    This is the home page for the
    <a href="https://railstutorial.jp/">Ruby on Rails Tutorial</a>
    sample application.
  </h2>

  <%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %>
</div>
<%#= image_tag("kitten.jpg", alt: "Kitten") %>
<%= link_to image_tag("rails.svg", alt: "Rails logo", width: "200px"),
                      "https://rubyonrails.org/" %>

最初のユーザー登録ページ(スタブ

<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<p>This will be a signup page for new users.</p>

プレビューで確認 成功

演習

1.名前付きルートsignup_pathを使えるようにしてください。

require 'test_helper'

class UsersControllerTest < ActionDispatch::IntegrationTest
  test "should get new" do
    get signup_path
    assert_response :success
  end

end
ubuntu:~/environment/sample_app (filling-in-layout) $ rails t
Running via Spring preloader in process 7679
Started with run options --seed 34459

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

Finished in 1.38496s
8 tests, 17 assertions, 0 failures, 0 errors, 0 skips

2.signupルートの部分をコメントアウトし、テスト red になることを確認してください。
確認できたら、コメントアウトを解除して green の状態に戻してください。

Rails.application.routes.draw do
  root 'static_pages#home'
  # 何の意味がわからん
  # ホーム画面がhomeページなるらしい
  get  '/help',    to: 'static_pages#help'
  # static_pagesコントローラからhomeアクションに紐付けされる
  # getでアクセルすることでページを取得することができる
  get  '/about',   to: 'static_pages#about'
  # 何を書いているかはわからない
  get  '/contact', to: 'static_pages#contact'
  # aboutアクションにGETリクエストを送る
  # get  '/signup',  to: 'users#new'
  # urlに/signupと書くとuserコントローラのnewアクションを起こす
end
ubuntu:~/environment/sample_app (filling-in-layout) $ rails t
Running via Spring preloader in process 7782
Started with run options --seed 17247

ERROR["test_layout_links", #<Minitest::Reporters::Suite:0x00007f63488cf520 @name="SiteLayoutTest">, 0.0667279179997422]
 test_layout_links#SiteLayoutTest (0.07s)
ActionView::Template::Error:         ActionView::Template::Error: undefined local variable or method `signup_path' for #<#<Class:0x00007f63487aa000>:0x00007f63487bb2d8>
            app/views/static_pages/home.html.erb:10
            test/integration/site_layout_test.rb:6:in `block in <class:SiteLayoutTest>'

ERROR["test_should_get_home", #<Minitest::Reporters::Suite:0x000055e490100750 @name="StaticPagesControllerTest">, 1.2288170709998667]
 test_should_get_home#StaticPagesControllerTest (1.23s)
ActionView::Template::Error:         ActionView::Template::Error: undefined local variable or method `signup_path' for #<#<Class:0x00007f63487aa000>:0x000055e490066a38>
            app/views/static_pages/home.html.erb:10
            test/controllers/static_pages_controller_test.rb:19:in `block in <class:StaticPagesControllerTest>'

ERROR["test_should_get_root", #<Minitest::Reporters::Suite:0x000055e48cd8e430 @name="StaticPagesControllerTest">, 1.2772767369997382]
 test_should_get_root#StaticPagesControllerTest (1.28s)
ActionView::Template::Error:         ActionView::Template::Error: undefined local variable or method `signup_path' for #<#<Class:0x00007f63487aa000>:0x000055e490127710>
            app/views/static_pages/home.html.erb:10
            test/controllers/static_pages_controller_test.rb:12:in `block in <class:StaticPagesControllerTest>'

ERROR["test_should_get_new", #<Minitest::Reporters::Suite:0x00007f63487a8f48 @name="UsersControllerTest">, 1.2946818399996118]
 test_should_get_new#UsersControllerTest (1.30s)
NameError:         NameError: undefined local variable or method `signup_path' for #<UsersControllerTest:0x00007f6348795498>
            test/controllers/users_controller_test.rb:5:in `block in <class:UsersControllerTest>'

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

Finished in 1.36400s
8 tests, 8 assertions, 0 failures, 4 errors, 0 skips

NameError: undefined local variable or method `signup_path'
signup_pathがないらしい。

3.合テストにsignupページにアクセスするコードを追加してください。
コードを追加したら実際にテストを実行し、結果が正しいことを確認してください。

require 'test_helper'

class SiteLayoutTest < ActionDispatch::IntegrationTest

 test "layout links" do
    get root_path
    assert_template 'static_pages/home'
    #正しいビューを描画しているかを調べている。
    assert_select "a[href=?]", root_path, count: 2
    #特定のリンクが存在するかどうかを調べている。
    assert_select "a[href=?]", help_path
    assert_select "a[href=?]", about_path
    assert_select "a[href=?]", contact_path
    get contact_path
    assert_select "title", full_title("Contact")
    get signup_path

    assert_select "title", full_title("Sign up")
  end
end

ubuntu:~/environment/sample_app (filling-in-layout) $ rails t
Running via Spring preloader in process 10568
Started with run options --seed 43272

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

Finished in 1.58374s
8 tests, 19 assertions, 0 failures, 0 errors, 0 skips
0
0
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
0