Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby on Rails | pry-debuggerでRailsのデバッグ

概要

pry-debuggerでRailsデバッグ

内容

ブレイクポイントとしてbinding.pryを設定した場合、
その場所限りでデバッグが可能でしたが、ステップイン・ステップアウトなどができません。
そこで、pry.navを追加すると移動操作が可能になります。

設定

Gemfile

gem "pry-debugger", "~> 0.2.2"

.pryrcに下記を追記してプロジェクトルート直下に保存

Pry.commands.alias_command 'c', 'continue'
Pry.commands.alias_command 's', 'step'
Pry.commands.alias_command 'n', 'next'
Pry.commands.alias_command 'f', 'finish'
Pry.commands.alias_command 'q', 'quit'

サンプル

実装コード
class SigninController < ApplicationController
  skip_filter :authorize, :except=>:index
  skip_filter :authorize, :except=>:signin

  def index
    flash[:message]
    render_partial message_display: true
  end

  def signin
    user = User.find_by name: params[:login]
    unless user && user.authenticate(params[:password])
      flash[:message] = "fail password authentication"
      render_partial message_display: true
      return redirect_to action: 'index'
    end

    binding.pry

    session[:user_id] = user.id
    render_partial menu_display: true, message_display: true
    redirect_to controller: 'user',action: 'list'
  end

  def signout
    session[:user_id] = nil
    flash[:message] = "ログアウトしました"
    render_partial message_display: true
    redirect_to :controller => 'signin', :action => 'index'
  end
end
デバッグ中の画面

サインイン時にブレイクポイントに到達し、自動的にpryが起動して
デバッグモードになります。
変数内容の表示や色の強調表示などpryの機能を確認出来ます。