#This was first published on http://blog.tamuztech.com by me.
We’ve have all been there where an error makes no sense and other solutions may not be relevant to your project. We’ll be talking about a handy gem which you can include in your rails app saving a lot of time debugging and querying online for relevant solutions.
Let’s look at an example
It looks to seem that we have an undefined
empty? method being called on
nil and that this error happens only when we try to create a checkbox.
There’s no obvious place in the code that’s displayed that calls
empty? and it seems that this exception page could be improved to give us a better idea as to where the error is thrown.
I generally add this on most of my rails app, not only does it look nicer, it has more functionality.
Adding this gem is just like any other but take care when adding this gem to your app it is important to place it in the [Development] group to avoid showing this errors on our applications production.
Now let’s get that error again but this time with Better_errors installed.
Better_errors show exactly where the error has occurred, and the relevant source code is also displayed nicely syntax-highlighted and we can see the empty? Method that is causing the problem.
Above this code is a stack trace and we can click any line of this to see the source code for that part of the stack. We can also look at the entire Rails app and view the source code for any part of the Rails source code or any gems that are part of the stack trace for the error.
Do you really need this?
For beginners a useful side-effect of this is that it helps us to get a better understanding of how Rails works. For our error we can see that the
check_box method is triggered in our code and working through the stack trace we’ll see that this method is called on a form builder and we can carry on and go through every step in the process to get a better understanding of what’s going on. At the bottom of the error page we’ll see information about the request including the parameters and any cookies that are used. This isn’t all that this gem can do, though. If we look at the error page we’ll see a tip to add a gem called
binding_of_caller which gives more functionality so let’s give that a try. Again, this gem should go in the development group and again we’ll need to run
bundle to install it.
This works for any line of the stack trace so we can view the local and instance variables at any point in the stack. This is really useful, but one of the greatest features is the interactive prompt which we can also use at any point in the stack trace. If we use this in the Task model we’ll see that the value of
nil which is why we’re getting an exception raised when we call
empty? on it.
empty? we should be calling
present? and to make this change we can click the name of the file which will open it up in a text editor. We’ll just make the change here but in a real application we’d write a failing test first to ensure that this bug is fixed and doesn’t recur.