LoginSignup
49
45

More than 5 years have passed since last update.

Railsのデバッグ環境等を整備する

Last updated at Posted at 2014-03-06

デバッグまわりのgemを整理してみました

エラー画面を見やすくする「better_errors」

無機質なRailsの標準のエラー画面が以下のように見やすくなります。

group :development, :staging do
  gem 'better_errors'       #追加
end

標準のエラー画面

スクリーンショット 2014-02-27 9.22.50.png

better_errorsを導入した場合のエラー画面

スクリーンショット 2014-02-27 9.27.15.png

エラー画面上でREPLが使えるようになる「binding_of_caller」

rails cで出来るような対話的な実行がエラー画面上から行えるようになります

group :development, :staging do
  gem 'better_errors'
  gem 'binding_of_caller'       #追加
end
  • 画面上で対話的にコードを実行できる スクリーンショット 2014-02-27 9.39.34.png

railsのコンソールを便利にする「pry-rails」

pry-railsを導入するとrailsのコンソール起動時にpryが起動するようになり、pryやpry-railsの便利な機能が使えます
pry-railsはpryに依存しているので下記のようにするとpryも同時にインストールされます

group :development, :staging do
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'pry-rails'           #追加
end

ブレークポイントをいれる

プログラム中に以下の記述をすると、ブレークポイントをいれてプログラムを止めることができます

   # GET /boards
   # GET /boards.json
   def index
     @boards = Board.all
pry.binding
   end

このように「pry.binding」と記述することでその位置でプログラムを止めてコンソールを使うことができます

Started GET "/boards" for 127.0.0.1 at 2014-03-04 09:28:21 +0900
Processing by BoardsController#index as HTML
[1] pry(#<BoardsController>)> p @boards
  Board Load (0.3ms)  SELECT "boards".* FROM "boards"
#<ActiveRecord::Relation [#<Board id: 1, name: "board1", created_at: "2014-02-27 00:19:18", updated_at: "2014-02-27 00:19:18">]>
=> [#<Board id: 1, name: "board1", created_at: "2014-02-27 00:19:18", updated_at: "2014-02-27 00:19:18">]
[2] pry(#<BoardsController>)> Board.all
  CACHE (0.0ms)  SELECT "boards".* FROM "boards"
=> [#<Board id: 1, name: "board1", created_at: "2014-02-27 00:19:18", updated_at: "2014-02-27 00:19:18">]

ルーティングを表示

show-routesでルーティングを表示できます

pry(main)> show-routes
       Prefix Verb   URI Pattern                   Controller#Action
    positions GET    /positions(.:format)          positions#index
              POST   /positions(.:format)          positions#create
 new_position GET    /positions/new(.:format)      positions#new
edit_position GET    /positions/:id/edit(.:format) positions#edit
     position GET    /positions/:id(.:format)      positions#show
              PATCH  /positions/:id(.:format)      positions#update
              PUT    /positions/:id(.:format)      positions#update
              DELETE /positions/:id(.:format)      positions#destroy
     stickies GET    /stickies(.:format)           stickies#index
              POST   /stickies(.:format)           stickies#create
   new_sticky GET    /stickies/new(.:format)       stickies#new
  edit_sticky GET    /stickies/:id/edit(.:format)  stickies#edit
       sticky GET    /stickies/:id(.:format)       stickies#show_______

モデルを表示

show-modelsでモデルを表示できます

pry(main)> show-models
Board
  id: integer
  name: string
  created_at: datetime
  updated_at: datetime
Position
  id: integer
  board_id: integer
  sticy_id: integer
  kpt_type: integer
  sequence: integer
  created_at: datetime
  updated_at: datetime
Sticky
  id: integer
  memo: string
  deleted: boolean
  created_at: datetime
  updated_at: datetime

pryのデバッグでステップ実行などを行なえる「pry-debugger」

ステップ実行などが出来るようになります

group :development, :staging do
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'pry-rails'
  gem 'pry-debugger'    #追加
end

以下のように定義するとショートカットを使えます

~/.pryrc
if defined?(PryDebugger)
  Pry.commands.alias_command 'c', 'continue'
  Pry.commands.alias_command 's', 'step'
  Pry.commands.alias_command 'n', 'next'
  Pry.commands.alias_command 'f', 'finish'
end

ルーティングをブラウザで表示できる「sextant」

sextantを導入すると、ルーティングをブラウザ上で確認できます。

group :development, :staging do
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'pry-rails'
  gem 'sextant'         #追加
end

http://localhost:3000/rails/routesにアクセスしてルーティングを確認できます。

スクリーンショット 2014-03-06 9.34.25.png

49
45
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
49
45