Two Useful Ruby Gems for Your Next Rails Project
Hello fellow coder! As I continue my coding journey with the Flatiron School, I am currently learning to build web-applications with the Ruby on Rails framework. It’s a great tool that is extremely intuitive and comes with so many different functions, which at times are a bit overwhelming. But if you’re anything like me, more functionality is better than less. There was just one part of the framework that bothered me and I wanted to be more clear, the error messages. You probably know what I’m talking about if you’ve used Rails and while working on your app, you go to your server to see an error message like this…
The error message is just ugly. The information is very useful for debugging, but it’s hard to read and interpret when it’s displayed in this manner. If are annoyed with these messages as much as I am, then I have good news for you, there is a SOLUTION!
better_errors
The first of the two gems, which I always include in my Gemfile before I begin my projects, is called BetterErrors! BetterErrors, replaces the basic set-up of Rails error messages with a nicer, more readable format. Just look at the same error message from above, but with the BetterErrors gem installed!
Wow! What a difference! The formatting of the error message is much easier to read and to navigate through! It provides a clear and specific error message above and lists other errors the server is encountering in your code below on the left hand side. The message also identifies the location of your error by showing and highlighting the code causing the issue. Showing the path to the error as well. Debugging just got a whole lot easier! Now let me show you how to install it in your Rails app with ease!
Installing better_errors
First, if you’re on your Rails app, start by opening your Gemfile, which contains all of the gems that were included when you created your new Rails app. Scroll down to the group of gems categorized under “development” and add: gem ‘better_errors’ at the bottom of the list, just like I did below.
Once you’ve added the gem, run ‘bundle install’ in your terminal and, voilà, you have just incorporated BetterErrors into your Rails project! Now anytime you run into errors or issues, you’ll be getting an error message that is more helpful and easier to follow. If you need more information or help with installation, I have linked the documentation to the BetterErrors gem here and up above! But wait… before you go build the next great app of our generation, I still have one more gem that I think will help you!
binding_of_caller
Once you have the BetterErrors gem installed, you could easily take it a run! It’s a very helpful tool that will assist you when you build your application. However, what makes BetterErrors REALLY cool is that it has more features! Queue drumroll please… Let me introduce you to binding of caller! If you remember working with pry to debug your code, you know what binding does. In short, binding an object encapsulates the execution context at a particular place in the program, which can be accessed at a later time when you call the function. Well, binding_of_caller will retrieve a binding of a method’s caller and also allow you to retrieve bindings even further up the stack! What’s nice about this gem is that it works in conjunction with BetterErrors. Let’s take a look at our error message again using BetterErrors.
If you look at our error message from BetterErrors, we have a very nice window with code on the right hand side that shows you and indicates where the error is located. Almost as if you placed a binding.pry right there… The only drawback is, we are unable to work with the binding object and debug. But, if you look at the ‘Tip’ included just below the window, you can see that it recommends the addition of the binding_of_caller gem! Let’s install it together!
Installing binding_of_caller
We’re just going to return to the Gemfile where we included our BetterErrors gem and add: gem ‘binding_of_caller’, after the BetterErrors gem. Then, you’ll run your ‘bundle install’ once more and check to see that it was installed.
Let’s take a look at our new and improved error message! You’ll notice that there is now a text field beneath the code containing the error. The binding_of_caller gem has given us the ability to test and debug right from the error message!
Final Remarks
Now how cool are these gems! These are just a few of a multitude of gems that can be used to help your Rails experience be a bit smoother. I really like using these two gems specifically, because not only do they work with one other, they also allow me to debug my code much more efficiently. I definitely make sure I include these two gems in EVERY Rails project that I work on. I really hope you try out these gems and see how useful they are for yourself! I also want to challenge you to look for other helpful gems that will assist you on your Rails journey, you would be surprised in what you find!
Check out, rubygems.org, to find and browse through a multitude of Ruby Gems. They also provide installation information and links to documentation for each gem!