如何在Rails 4.2中正确设置Pry

时间:2015-08-09 12:28:35

标签: debugging ruby-on-rails-4.2 pry pry-rails

我很担心需要安装哪些宝石才能在Rails 4.2项目中正确运行Pry。直到最近我似乎都很好。出于某种原因,每当我尝试在命令行中使用binding.prynext等步进控制台会话时,step现在似乎都会吐出来。它将抛出一个丑陋的堆栈跟踪,然后在代码中向我显示相同的点,而不会进一步踩踏。在第二个next命令之后,它会抛出相同的堆栈跟踪并退出,从而将执行返回到localhost服务器。我在No frames collected的浏览器页面上收到运行时错误。

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

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

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...

第二节与第一节相同 - 此时执行返回服务器。

这是我的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宝石评论出来......没有区别。

最后,我的.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

1 个答案:

答案 0 :(得分:2)

一些摆弄并且几次服务器重启似乎已经解决了这个问题。感谢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