2020/12/23 補講課題
ようやくrailsチュートリアル一周したので、RUNTEQのチェックテスト前の補講課題
コマンド実行して環境構築
$ git checkout -b fix_[自分のアカウント名] #作業ブランチ作る $ rbenv local 2.6.6 #rubyのバージョンを指定 $ nodenv local 12.14.0 Node.jsのバージョンを指定 サーバサイドでJavaScript動かすらしいけど何ができるのか全然わからない $ bundle install --without production $ yarn install #javascriptのパッケージマネージャ,ライブラリの追加やインストールを容易にしてくれるやつ $ rails db:migrate $ rails server
最初からエラーが出る、やだ
An error occurred while installing puma (4.3.3), and Bundler cannot continue. Make sure that `gem install puma -v '4.3.3' --source 'https://rubygems.org/'` succeeds before bundling.
以下のコードでいけるとのことでまんまやってみた
$ gem install puma:4.3.3 -- --with-cflags="-Wno-error=implicit-function-declaration"
これを参考
【puma】puma4台インストールできない問題の対処法 - Qiita
rails severでもエラーでた
======================================== Your Yarn packages are out of date! Please run `yarn install --check-files` to update. ======================================== To disable this check, please change `check_yarn_integrity` to `false` in your webpacker config file (config/webpacker.yml).
書かれてる通りwebpacker.ymlを変更したら大丈夫だった
パッケージが古くなっていたらエラーになってパッケージを最新の状態に保つ手助けになるオプションとのこと
晴れてserverも立ち上がったので、とりあえず最初のテスト
安川先生がいなくて不安・・
とりあえずエラーを書き出してみる
Error: UserTest#test_success_to_visit_about_page: NameError: undefined local variable or method `about_url' for #<UserTest:0x00007fd0ada5dd60> test/system/user_test.rb:49:in `block in <class:UserTest>' Error: UserTest#test_login_failed_with_not_exist_user: NoMethodError: undefined method `authenticate' for nil:NilClass app/controllers/sessions_controller.rb:6:in `create' Error: UserTest#test_logout_success: NoMethodError: undefined method `log_in' for #<SessionsController:0x00007fd09899f5c8> Did you mean? login_url Did you mean? login_url app/controllers/sessions_controller.rb:7:in `create' Error: UserTest#test_login_success_with_exist_user: NoMethodError: undefined method `log_in' for #<SessionsController:0x00007fd0c81e8ae8> Did you mean? login_url Did you mean? login_url app/controllers/sessions_controller.rb:7:in `create' Failure: UserTest#test_signup_success_with_valid_user [/Users/workplace/runteq/Exam_RailsBasic_01/test/system/user_test.rb:37]: expected to find text "Signup success!" in "RUNTEQ APP\nLog in\nSign up\nThe form contains 3 errors.\nPassword can't be blank\nPassword is too short (minimum is 6 characters)\nPassword can't be blank\nName\nEmail\nPassword\nConfirmation"
テスト一個ずつ見ていく
test "success to visit about page" do visit about_url assert_text "StaticPages#about" end
routes.rbを修正して、コントローラにアクション追加して、about.rbのファイル名修正したんだけどこれでいいのか?
もう一回テストしたらエラーメッセージ異常になったけど一旦無視
test "login failed with not exist user" do visit login_url fill_in "Email", with: "other_user@example.com" fill_in "Password", with: "foobar" click_button "Log in" assert_no_text "Example User" end
これはnilガードっぽいのでsessionsコントローラのcreateアクションを修正
test "logout success" do login click_on "Account" click_on "Log out" assert_text "Log in" end
log_inメソッドがあるsessionhelperをincludeしてみる
test "signup success with valid user" do visit signup_url fill_in "Name", with: "Another User" fill_in "Email", with: "another_user@example.com" fill_in "Password", with: "foobar" fill_in "Confirmation", with: "foobar" click_button "Create my account" assert_text "Signup success!" assert_text "Another User" end
最後のはvalidation、userコントローラのcreateアクション、singupのテンプレートあたりがおかしそう
と思ったらstrong_parameterにpasswordがなかった
修正したけど、エラー出る
userコントローラでもlog_inメソッド使いたいとのことなので、includeをapplicationcontrollerに描き直し
で終わり!
解説動画
眠いけど1時間頑張って見る!
- git hub 右上からclone用のURLが入手できる
- mkdir -pでないフォルダとかも作成される、前も出てきたけどコマンドすぐ忘れてしまう
- ブラウザの検証ツール ネットワークタブでなんのデータを送ったかとかが見れる!便利そう
- binding.irbをアクションの中に組み込むと、そこで止まる
serverを立ち上げているターミナルでその時点のフォームの情報とかが確認できる
ここでuser = User.new(user_params)すると、エラーに引っかかる原因を割り出せるかも
さっき大分目視で見つけたから、こういうやり方覚えていきたい - command + shift + F、grep -r 文字列 で検索できる!
さっきは普通に探し回った - findはエラーを返す、find_byはnilを返す
- ぼっち演算子 user&.autenticate(params[....)慣れない
- def new; end など1行で書くとデフォルトのテンプレート表示するだけのメソッドってことでOKですよ、ということを明示できる
- ターミナルでhistoryコマンド → ctrl + R で使ったコマンドの履歴が出せる、検索できる
- better errors (gem)
- ps 今動いているプロセスを確認→killコマンドで停止