Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?

Rails Tutorial (9/2)

内容

9.2.3 Friendly forwarding

  • ログインした後にページ遷移されるデフォルトの表示はユーザーのプロフィールページになる。
  • session_helperにこの2つを追加する。
    • store_location requestオブジェクトにより、ユーザーが本来遷移したかったページのURLを取得し、return_toをキーとしてsession情報をcookiesとして保存しておく。
    • redirect_back_or ユーザーが遷移したい情報があればその場所に、なければデフォルトにページを遷移させる。

問題

テストでエラーが表示されました。

% bundle exec rspec spec/                                                                                                                                                                   (git)-[updating-users]
.......FFFFFFFF..................................FFFFFFFFFF........

Failures:

  1) User pages edit page 
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/support/utilities.rb:38:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:61:in `block (3 levels) in <top (required)>'

  2) User pages edit page 
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/support/utilities.rb:38:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:61:in `block (3 levels) in <top (required)>'

  3) User pages edit page 
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/support/utilities.rb:38:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:61:in `block (3 levels) in <top (required)>'

  4) User pages edit with valid information 
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/support/utilities.rb:38:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:61:in `block (3 levels) in <top (required)>'

  5) User pages edit with valid information 
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/support/utilities.rb:38:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:61:in `block (3 levels) in <top (required)>'

  6) User pages edit with valid information 
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/support/utilities.rb:38:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:61:in `block (3 levels) in <top (required)>'

  7) User pages edit with valid information 
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/support/utilities.rb:38:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:61:in `block (3 levels) in <top (required)>'

  8) User pages edit with valid information 
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/support/utilities.rb:38:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:61:in `block (3 levels) in <top (required)>'

  9) Authentication signin with invalid information 
     Failure/Error: before { click_button "Sign in" }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/requests/authentication_pages_spec.rb:11:in `block (4 levels) in <top (required)>'

  10) Authentication signin with invalid information 
     Failure/Error: before { click_button "Sign in" }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/requests/authentication_pages_spec.rb:11:in `block (4 levels) in <top (required)>'

  11) Authentication signin with invalid information after visiting another page 
     Failure/Error: before { click_button "Sign in" }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/requests/authentication_pages_spec.rb:11:in `block (4 levels) in <top (required)>'

  12) Authentication signin with valid information 
     Failure/Error: before { valid_signin(user) }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/support/utilities.rb:6:in `valid_signin'
     # ./spec/requests/authentication_pages_spec.rb:24:in `block (4 levels) in <top (required)>'

  13) Authentication signin with valid information 
     Failure/Error: before { valid_signin(user) }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/support/utilities.rb:6:in `valid_signin'
     # ./spec/requests/authentication_pages_spec.rb:24:in `block (4 levels) in <top (required)>'

  14) Authentication signin with valid information 
     Failure/Error: before { valid_signin(user) }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/support/utilities.rb:6:in `valid_signin'
     # ./spec/requests/authentication_pages_spec.rb:24:in `block (4 levels) in <top (required)>'

  15) Authentication signin with valid information 
     Failure/Error: before { valid_signin(user) }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/support/utilities.rb:6:in `valid_signin'
     # ./spec/requests/authentication_pages_spec.rb:24:in `block (4 levels) in <top (required)>'

  16) Authentication signin with valid information 
     Failure/Error: before { valid_signin(user) }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/support/utilities.rb:6:in `valid_signin'
     # ./spec/requests/authentication_pages_spec.rb:24:in `block (4 levels) in <top (required)>'

  17) Authentication signin with valid information followed by signout 
     Failure/Error: before { valid_signin(user) }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/support/utilities.rb:6:in `valid_signin'
     # ./spec/requests/authentication_pages_spec.rb:24:in `block (4 levels) in <top (required)>'

  18) Authentication authorization for non-signed-in users when attempting to visit a protected page after signing in should render the desired protected page
     Failure/Error: click_button "Sign in"
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/requests/authentication_pages_spec.rb:49:in `block (5 levels) in <top (required)>'

Finished in 1.3 seconds
67 examples, 18 failures

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:66 # User pages edit page 
rspec ./spec/requests/user_pages_spec.rb:68 # User pages edit page 
rspec ./spec/requests/user_pages_spec.rb:67 # User pages edit page 
rspec ./spec/requests/user_pages_spec.rb:86 # User pages edit with valid information 
rspec ./spec/requests/user_pages_spec.rb:83 # User pages edit with valid information 
rspec ./spec/requests/user_pages_spec.rb:85 # User pages edit with valid information 
rspec ./spec/requests/user_pages_spec.rb:84 # User pages edit with valid information 
rspec ./spec/requests/user_pages_spec.rb:82 # User pages edit with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:13 # Authentication signin with invalid information 
rspec ./spec/requests/authentication_pages_spec.rb:14 # Authentication signin with invalid information 
rspec ./spec/requests/authentication_pages_spec.rb:18 # Authentication signin with invalid information after visiting another page 
rspec ./spec/requests/authentication_pages_spec.rb:35 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:32 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:34 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:33 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:31 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:28 # Authentication signin with valid information followed by signout 
rspec ./spec/requests/authentication_pages_spec.rb:54 # Authentication authorization for non-signed-in users when attempting to visit a protected page after signing in should render the desired protected page

Randomized with seed 50404

解決

Exercises8の際に、app/controllers/sessions_controller.rbにおいてform_for,からform_tag変更を加え、:sessionを消したはずなのにまた追加してしまったことが原因でした。 sessionを消し、結果テストが通るようになりました。

% bundle exec rspec spec/                                                                                                                                                                   (git)-[updating-users]
...................................................................

Finished in 1.57 seconds
67 examples, 0 failures

Randomized with seed 16639

9.3 Showing all users

ユーザー一覧が見れるページをつくる! その際にユーザー増えすぎたりしたときのためにページングの機能を追加させたり、ユーザーを削除したりする権限をもつadminを作ったりする。

9.3.1 User index

  • 誰でも閲覧可能なのはshowページ

  • 登録しているユーザーだけ見れるのはindexページ

  • before_filter

    • アクションメソッドが実行される前に、なんらかの処理を行いたい場合

9.3.2 Sample users

  • faker Gem

    • Rubyで簡単にダミーデータを作成することができるGemライブラリ
  • db:populate

    • localのrails環境で試験的なDBをつくり、100人分のsample users を作成する

9.3.3 Pagination

  • will_paginate

    • pagenationしてくれる君(Gem)
  • current user が最初のページに存在しているかをチェックする必要有り

  • Factory Girl で一つ一つ手作業で作って、テストをして・・なんてのは愚行

    • hard-codeで一つづつ書いていくよりも、sequenceメソッドで任意にユーザーをつくれるようにするべき。
  • before(:all) が実行されるのは最初の 1 回だけ。pagenationが行われるたびに、30人のユーザーを作っていたら、処理が遅くなってしまう。

    • :eachで実行した場合は勝手にテスト終了後Rollbackされるが、:allの時はRollbackされない。作ったデータはそのまま残る。そのため、after(:all) で全てを消してあげる必要がある。
rails console でチェック
% rails console                                                                                                                        (git)-[updating-users]
Loading development environment (Rails 4.0.8)
irb(main):001:0>  User.paginate(page: 1)
  User Load (3.3ms)  SELECT "users".* FROM "users" LIMIT 30 OFFSET 0
   (1.0ms)  SELECT COUNT(*) FROM "users"
=> #<ActiveRecord::Relation [#<User id: 1, name: "Example User", email: "example@railstutorial.org", created_at: "2014-09-02 07:46:28", updated_at: "2014-09-02 07:46:28", password_digest: "$2a$10$LMvNz43uk.HEBsB9XrWR3ubHLhjmYLcpLTDmGTbi2sZA...", remember_token: "871cd4d1a718d4aa34b1f7e2498fa818e69a3389">, #<User id: 2, name: "Zackary Hegmann", email: "example-1@railstutorial.org", created_at: "2014-09-02 07:46:28", updated_at: "2014-09-02 07:46:28", password_digest: "$2a$10$FJw4DKlMgb9eHXQkaaGp8OHpf2TyLRdoJLI71TmimU0I...", remember_token: "505ee70fac95636ce0b10f6a1e8bf10e582d2d16">, #<User id: 3, name: "Mr. Jayson Crona", email: "example-2@railstutorial.org", created_at: "2014-09-02 07:46:28", updated_at: "2014-09-02 07:46:28", password_digest: "$2a$10$RqDJsWcFGZrStoGU6ekmxuCm0TPPu0ibGW/0eFTI5eUz...", remember_token: "99289a6fc3a558906524427d1ed34c1fbbd9fe96">, #<User id: 4, name: "Judy Hahn", email: "example-3@railstutorial.org", created_at: "2014-09-02 07:46:28", updated_at: "2014-09-02 07:46:28", password_digest: "$2a$10$iuEMla8pgJFoK4ryTDH/d.fW109Fq11gbXBY.1i/F1En...", remember_token: "38ee3c2b51697a125fd8be4806a059f6c9b16500">, #<User id: 5, name: "Dr. Oral Kub", email: "example-4@railstutorial.org", created_at: "2014-09-02 07:46:28", updated_at: "2014-09-02 07:46:28", password_digest: "$2a$10$o7jo3H.tMmXYT56B4hp1KukssBVSHQ.S364/9rN8vrTj...", remember_token: "4eb45623a07af42654991e014a0f52a33663edb1">, #<User id: 6, name: "Amparo Heathcote", email: "example-5@railstutorial.org", created_at: "2014-09-02 07:46:28", updated_at: "2014-09-02 07:46:28", password_digest: "$2a$10$GDKQIS/PJGwML1nvK046z.UitOtjdWXLnhhyROpfhTXZ...", remember_token: "5e44cf796442f14730987e563ad128c6d19702fc">, #<User id: 7, name: "Annabel Herzog", email: "example-6@railstutorial.org", created_at: "2014-09-02 07:46:28", updated_at: "2014-09-02 07:46:28", password_digest: "$2a$10$IztwkYnBExOXwaggaesMHOLpiwKLa.nZfSpk1VOuo6ny...", remember_token: "7ad94610051aca19fb3bd00fafc52d2de5630e81">, #<User id: 8, name: "Libbie Bernhard", email: "example-7@railstutorial.org", created_at: "2014-09-02 07:46:29", updated_at: "2014-09-02 07:46:29", password_digest: "$2a$10$ymzdnbOBkTWN0n8VPp8mtuSpRy0zpbAF.P6vncVw0T5V...", remember_token: "e715851afb40006fceef9cc1d3c6845b47331ece">, #<User id: 9, name: "William McCullough", email: "example-8@railstutorial.org", created_at: "2014-09-02 07:46:29", updated_at: "2014-09-02 07:46:29", password_digest: "$2a$10$kqc39EWnhgs9u6PcUQC4EeoCoPxOKPTAJFtvJ9eetimt...", remember_token: "ddc7a06bc952b0e5e4afb3bb8b22995fc789a196">, #<User id: 10, name: "Shemar Christiansen", email: "example-9@railstutorial.org", created_at: "2014-09-02 07:46:29", updated_at: "2014-09-02 07:46:29", password_digest: "$2a$10$YiEHry6/H2zztp0/hMi6y.YuYRc/alDX553lOw6kRsco...", remember_token: "738100a57cd11c4cd45068770d31abc9b30f3614">, ...]>
  • User.paginateは

備考

Vimがクラッシュしてしまった場合について
  • swpファイルとは? vimエディタでは、ユーザーがオリジナルのファイルから変更した情報やアンドゥ用の情報を swapファイルに格納しており、 もしなんらかの原因で編集情報を保存する前にvimエディタが強制終了したとしても、 オリジナルのファイルと、このswapファイルがあれば、強制終了前の状態を復元できます

http://nanasi.jp/articles/howto/file/crash-recovery.html#id6

ボスケテ

  • penultimateとは何でしょう? rails tutorial (9.3 Showing all users 文章の冒頭部分)

http://www.railstutorial.org/book/updating_and_deleting_users#sec-showing_all_users

英単語

  • blemish 傷,欠陥
  • company 仲間
  • liberty 自由に
  • opposite 反対側に
  • thoroughly 徹底的に
  • terribly 恐ろしく,非常に,ひどく
  • cumbersome (重くて)扱いにくい、やっかい
  • replacement 交換,取り替え
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment