LoginSignup
0
1

More than 3 years have passed since last update.

binding.pryの使い方

Last updated at Posted at 2019-10-02

デバッグツールのbinding.pryの基本的な使い方の備忘録

  1. gemfileにbinding.pryを記述
  2. ターミナルでbundle insatall を実施する
  3. 動作を止めたいところ(変数の中身を確認したいところ)に挿入する。
user_contorller.rb
def login
    @user = User.find_by(
      email:params[:email],
      password:params[:password]
    )
    binding.pry
    if @user
      flash[:notice] = "ログインしました"
      redirect_to action: :index
    else
      @error_message = "メールアドレスまたはパスワードが間違っています"
      @email = params[:email]
      @password = params[:password]
      render action: :login_form
    end  
  end

アクションに該当する動作を実施するとターミナルに以下のように表示される。

    40: def login
    41:   @user = User.find_by(
    42:     email:params[:email],
    43:     password:params[:password]
    44:   )
    45:   binding.pry
 => 46:   if @user
    47:     flash[:notice] = "ログインしました"
    48:     redirect_to action: :index
    49:   else
    50:     @error_message = "メールアドレスまたはパスワードが間違っています"
    51:     @email = params[:email]
    52:     @password = params[:password]
    53:     render action: :login_form
    54:   end  
    55: end

あとはterminalで確認したい中身の変数をたたけば確認できる!


[1] pry(#<UsersController>)> @user
=> nil
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