我一直在许多Rails应用程序中工作,在此期间我发现了很多错误,但有一件事真让我烦恼的是显示终端中Ruby文件名的错误。
我试图运行
User.find(3)
在Rails控制台中,虽然我没有多个用户记录,但它显然出现了错误,我意识到了这一点。但位置部分真的搞砸了我的终端。
以下是错误日志:
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=3
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.0.beta2/lib/active_record/core.rb:150:in `find'
from (irb):79
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:110:in `start'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:9:in `start'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands.rb:17:in `<top (required)>'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `block in require'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /home/travis/tumpy/bin/rails:8:in `<top (required)>'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `block in load'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /home/travis/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/travis/.rvm/rubies/ruby-
我期望的解决方案是隐藏位置部分并仅显示错误,显示
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=3
答案 0 :(得分:2)
IRB有一个back_trace_limit
选项,默认为16
:
irb(main):001:0> IRB.CurrentContext.back_trace_limit
#=> 16
将其设置为0
会抑制整个回溯:
irb(main):002:0> IRB.CurrentContext.back_trace_limit = 0
#=> 0
irb(main):003:0> User.find(0)
# User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 0 LIMIT 1
#ActiveRecord::RecordNotFound: Couldn't find User with id=0
irb(main):004:0>
您也可以将其作为命令行选项传递:
$ rails console -- --back-trace-limit=0