0
0

More than 1 year has passed since last update.

binding.pry

Posted at

binding.pry

Gemのpry-railsを追加することで使える機能。
binding.pryを記入した処理を、ローカルサーバーで実行すると記入された場所で処理が中断される。

エラー → 仮説 → デバッグ

今回の場合は、
エラー  : NoMethodError in Tweets#index
仮説   : formでパラメーターが送れていない可能性がある
デバッグ : binding.pryでparamsをチェック
という流れでデバッグする。

  # ツイート投稿ボタンが押され、createされる処理
  def create
+   binding.pry
    Tweet.create(tweet_params)
  end

ターミナルを確認

処理が止まり、irbのように対話可能の状態になる

Started POST "/tweets" for ::1 at 2020-03-18 17:31:23 +0900
Processing by TweetsController#create as HTML
  Parameters: {"authenticity_token"=>"7imvHaB3Z9fle2uEDeNY5mBJebreoWRs6Scs8zcEHuL5FHWhXS8eOpX8Db8i13SGuV1F8T9fTbgSCuBFkrOmDQ==", "tweet"=>{"image"=>"", "text"=>"aaa"}, "commit"=>"SEND"}
  User Load (0.8ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  ↳ app/controllers/tweets_controller.rb:50:in `move_to_index'

From: /Users/div-M.T/projects/pictweet/app/controllers/tweets_controller.rb @ line 14 TweetsController#create:

    13: def create
 => 14:   binding.pry
    15:   Tweet.create(tweet_params)
    16: end

[1] pry(#<TweetsController>)> 

paramsと入力すれば、ブラウザで入力フォーム(form)から送信したパラメーターが配列の形で出力できる。

[1] pry(#<TweetsController>)> params
=> <ActionController::Parameters {"authenticity_token"=>"7imvHaB3Z9fle2uEDeNY5mBJebreoWRs6Scs8zcEHuL5FHWhXS8eOpX8Db8i13SGuV1F8T9fTbgSCuBFkrOmDQ==", "tweet"=>{"image"=>"", "text"=>"aaa"}, "commit"=>"SEND", "controller"=>"tweets", "action"=>"create"} permitted: false>
[2] pry(#<TweetsController>)> 
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