overheadwithcups.jpg

Blog

Better Errors: Debugging Rails using two cool gems!

Many years ago when I was first learning to ski, I could barely slide five feet without falling down.  It was incredibly frustrating –to the point where I badly wanted to quit.  A sympathetic instructor, seeing my great frustration, said, “Don’t worry about the mistakes.  Even the best skiers constantly make mistakes.  The difference is that they know how to correct for them before they fall or lose control.”

I would give similar advice to any new coder.  Don’t worry about your mistakes as even the most experienced coders frequently make mistakes.  We mistype words; we forget to bundle or rake; we name the same variable differently in different files.  You name it, we do it!  Constantly!

The difference is an experienced coder quickly recognizes the mistake and knows how to correct it.  So go boldly ahead and make your mistakes!  Just learn to recognize the error messages that come with them so you can quickly correct your mistakes and move on.

One help for coders at any level is having tools that assist in debugging problems as they arise.  Here are a few tools you may want to incorporate into your workflow.

Let’s start with the tool built into Rails, a gem called “byebug”.  Learning to use byebug effectively is good first step for new coders.  It is simple to use.  Put the line “byebug” any place in your code and your app will pause any time the line is encountered. [Note: Do not include the quotes.  Also, in any “erb” files, you use “<% byebug %>, of course!]  When your app hits the byebug line, it will pause and open an irb-type console in your rails server window.  Observe that five lines of code before and after byebug are displayed.  From here you can type ‘continue” to simply move on.  More importantly, you have couple of dozen commands to invoke to better understand the current state of the app.  Here is a link to help get started: Debugging Ruby with Byebug

When we screw up, Rails provides its bright red error pages to let us know.  It is important to learn to read and understand these messages, but often they are cryptic and less helpful than they could be.  Better error messages would be helpful!  

One gem I always include in my Rails apps is called “better_errors”.  You’ll also want to install its buddy, “binding_of_caller”.  Together they provide exactly what is claimed: better error messages.  Significantly, better-errors also provides an irb type interface that allows one to inspect the status of local and instance variables so a coder can access both their class (e.g., string, array etc.) and their current value.  Finally better-errors also provides a full stack trace helping the coder to determine exactly where in the code things go wrong.  Better_errors is a great gem to add to any project and for newbies is even simpler to learn and use than byebug.  A good starting point to learn more is here: Debugging Rails with the Better Errors gem.

There are many other gems to help coders debug their Rails apps, including “pry-rails”, “hirb” among others, but learning and using the two discussed above will get you off to a great start!

SHARE THIS STORY | |

Search

Subscribe to Blog