如何在 Rails 4.2 中正确设置 Pry

How to correctly set up Pry in Rails 4.2

我很困惑需要安装哪些 gem 才能正确 运行 在 Rails 4.2 项目中撬动。直到最近,我似乎还过得很好。出于某种原因,每当我尝试在命令行中使用 nextstep 等单步执行控制台会话时,binding.pry 现在似乎会吐出。它会抛出一个丑陋的堆栈跟踪,然后在代码中显示相同的点,而不是进一步。在第二个 next 命令之后,它抛出相同的堆栈跟踪并退出,将执行返回到本地主机服务器。我在 No frames collected 的浏览器页面上收到 运行 时间错误。

如果我在代码中注释掉 binding.pry,一切正常。所以我强烈怀疑我的 Pry 配置有问题。似乎有几十种 Pry 变体,没有明确的指导说明应该安装哪个(哪些),哪个可能会发生冲突等。

这是我在代码到达 binding.pry 调用后在 Pry 控制台中调用 next 时获得的堆栈跟踪:

From: /Users/me/sites/arailsapp/app/controllers/bars_controller.rb @ line 31 BarsController#edit:

    29: def edit
    30:   binding.pry
 => 31:   @bar = Bar.find(params[:id])
    32:   @foo = @bar.foo
    33: end

[6] pry(#<BarsController>)> n
Completed 500 Internal Server Error in 2685ms (ActiveRecord: 0.0ms)

RuntimeError - No frames collected.:
  pry-byebug (3.2.0) lib/byebug/processors/pry_processor.rb:122:in `perform_next'
  pry-byebug (3.2.0) lib/byebug/processors/pry_processor.rb:60:in `perform'
  pry-byebug (3.2.0) lib/byebug/processors/pry_processor.rb:49:in `run'
  pry-byebug (3.2.0) lib/byebug/processors/pry_processor.rb:111:in `resume_pry'
  pry-byebug (3.2.0) lib/byebug/processors/pry_processor.rb:69:in `at_line'
  byebug (5.0.0) lib/byebug/context.rb:90:in `at_line'
  app/controllers/scholarships_controller.rb:31:in `edit'
  actionpack (4.2.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.2.1) lib/abstract_controller/base.rb:198:in `process_action'
  actionpack (4.2.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.2.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.2.1) lib/active_support/callbacks.rb:117:in `call'

  # omitting the next ~40 lines in the Rails stack...

第二节看起来与第一节相同 - 此时执行 returns 到服务器。

这是我的 Gemfile:

source 'https://rubygems.org'

ruby '2.2.0'

gem 'rails', '4.2.1'
gem 'pg', '~> 0.18.2'
gem 'haml-rails', '~> 0.9.0'
gem 'sass-rails', '~> 5.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'will_paginate', '~> 3.0.7'
gem 'will_paginate-bootstrap'
gem "nilify_blanks"
gem 'filterrific', '~> 2.0.5'
gem 'chardinjs-rails'

# Install bootstrap and associated gems
gem 'bootstrap-sass', '~> 3.3.4.1'
gem 'autoprefixer-rails', '~> 5.2.0'

# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'

group :production do
  gem 'rails_12factor', '~> 0.0.3'
end

group :assets do
  gem 'uglifier', '>= 1.3.0'
  gem 'coffee-rails', '~> 4.1.0'
end

group :development do
  gem 'better_errors', '~> 2.1.1'
  gem 'annotate', '~> 2.6.10'
end

group :development, :test do
  gem 'pry-rails'
  gem 'pry-stack_explorer'      
  gem 'pry-byebug'
  gem 'web-console', '~> 2.0'
  gem 'spring'
  gem 'spring-commands-rspec', '~> 1.0.4'
  gem 'rspec-rails', '~> 3.2.3'
  gem 'guard-rspec', '~> 4.6.0'
  gem 'sqlite3'
  gem 'factory_girl_rails', '~> 4.5.0', require: false
end

group :test do
  gem 'database_cleaner', '~> 1.4.1'
  gem 'faker', '~> 1.4.3'
  gem 'capybara', '~> 2.4.4'
  gem 'launchy', '~> 2.4.3'
  gem 'shoulda', '~> 3.5.0'
end

我也试过 pry-stack_explorerpry-byebug gem 被注释掉了...没有区别。

最后,我的 .pryrc 文件:

# ~/.pryrc
if defined?(PryByebug)
  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

# Hit Enter to repeat last command
Pry::Commands.command /^$/, "repeat last command" do
  _pry_.run_command Pry.history.to_a.last
end

一些摆弄和几次服务器重启似乎已经解决了这个问题。感谢 Deivid 建议删除 pry-stack_explorer。这是测试和开发组的 Gemfile:

group :development do
  gem 'better_errors', '~> 2.1.1'
  gem 'annotate', '~> 2.6.10'
end

group :development, :test do
  gem 'pry-rails'
  gem 'pry-byebug'
  gem 'web-console', '~> 2.0'
  gem 'spring'
  gem 'spring-commands-rspec', '~> 1.0.4'
  gem 'rspec-rails', '~> 3.2.3'
  gem 'guard-rspec', '~> 4.6.0'
  gem 'sqlite3'
  gem 'factory_girl_rails', '~> 4.5.0', require: false
end

group :test do
  gem 'database_cleaner', '~> 1.4.1'
  gem 'faker', '~> 1.4.3'
  gem 'capybara', '~> 2.4.4'
  gem 'launchy', '~> 2.4.3'
  gem 'shoulda', '~> 3.5.0'
end