开发者

How do I debug an error in a gem?

I'm getting this error with the Paperclip gem.

NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.size):
  paperclip (2.3.8) lib/paperclip/attachment.rb:104:in `assign'

How can I debug this, seeing as thou开发者_JS百科gh it's in a gem? If this was my code I would place a few puts to see what's going on, but I can't do that here. I'm using Rails 3.0.1


here's my best shot.

  • Open one of your controllers and add a debugger line to it. We need the debugger to kick in to set the breakpoint
  • Next run rails --debug
  • navigate to the page / controller with the breakpoint
  • the debugger console will appear.
  • run Gem.find_files("attachment.rb") <- I'll call $GEM_ROOT to the path that this returns
  • run list $GEM_ROOT/lib/paperclip/attachment.rb:104 - this will show the code around the area where the error occurred.
  • run b $GEM_ROOT/lib/paperclip/attachment.rb:$LINE - replace $LINE for a good candidate for the breakpoint.
  • run cont
  • hit the page / action that causes the error, and the debug console should open at the break point.

I think there must be a way to get the path to the gem file programmatically, but I have no idea how to do it :(


The accepted answer works; however, it is as simple as going to the gem source and inserting the debugger command where you would like to break.

Be careful about whether your inserted debugger command is reached when rails serves the request you are trying. With a controller, you might not be entering the controller right away. There are sometimes before filters or helper methods that shortcut the controller and return. Think of the devise gem before_filter :authenticate_user! as an example.

If you use bundler, and most of us currently do, a bundle show paperclip command issued from your project root directory will show you where to find the source code for the paperclip gem.

Also, you need to restart the server after inserting the debugger statement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜