7 must have Development Gems to install on every project



Have you wasted a few hours debugging some condition only to find a query or function something was returning nil? Spent time optimizing queries only to forget to do eager loading? Scrolled through your development log past 100s of lines for requests for images, css and javascript that you don’t really need to refer? We have all been there and these 7 gems solve exactly these problems. Once you learn how to use them you’ll find that you can accomplish what sometimes takes hours in minutes.

One of the largest portions of time that you will spend developing an application is on debugging and optimizing. As the rails application becomes bigger with more moving parts, debugging it becomes slower and finding parts that are causing your application to be slow become fewer inbetween.

Awesome Print

Rails objects can get pretty dense and hard to read when inspecting in the console. Awesome print solves this problem by prettifying the output. Read on to see by what I mean.

Lets run the command User.last in the rails console. This is something most of us do on a day to day basis in the rails console.

Normal console without Awesome Print installed

As you can see on the Terminal above, the output is kind of hard to read although the object is not that complex. Awesome print takes that output it and makes it much easier to read. Lets install the gem and see how it changes.

  # Gemfile
  gem "awesome_print", require:"ap"

Make sure to include the require:"ap" option, bundle install and restart the rails console. It will not work without it.

We run the same command, however this time we just run the command by adding ap to the beginning of the commend. So it becomes ap User.last. Basically what you are doing is passing to the ap method the object that you want it to print.

Normal console without Awesome Print installed

As you can see awesome print make a huge difference to the readability of the output. Now in the console pass any object into ap to get the prettified output. Another cool feature is that awesome print will give you the index numbers when passing in arrays.

  ap User.last

There is also a way that you make awesome print the default, when outputting any object in the console. For that please refer to the documentation on Github. However the reason that I choose not to go with it is that it will error out if the gem is not installed.

Rails Panel

Rails panels adds a number of useful features to the Google Chrome inspector. To use this you will need to install the meta_request gem and also install the Chrome extension by going here.

Railspanel is something I keep switched on pretty much all the time if I’m doing any backend work. It gives you all the metrics and information required without actually having to go into the Rails console.

The first columns gives you the HTTP Response Status, the Controller and Action that your route hit, the HTTP Verb (PUT/POST/GET), request format (HTML, JSON, XML) and the response time. These metrics by itself would have been quite valuable, but there is more. You can click on any request that you made and see more information about it.

I find the ActiveRecord tab to be extremely useful as it gives you the raw SQL queries being executed and the executing time for that query. It also gives you the number of queries that were used for that request which is great for optimizing the app. Apart from that, clicking on the controller name automatically opens your editor and takes you to that line of code!

The next tab is the Rendering which gives a breakdown of all the views being rendered. This is useful for finding which partials are taking the longest to render. Click on the view name automatically opens it in your editor.

Peek

Made by GitHub, great for getting the most important metrics such as number of SQL queries / time taken to execute, current branch, number of Redis workers etc. There are number of plugins and extensions that add all sorts of metrics and you can even write your own. One nice features is that it can be enabled in the production environment for select people. So you can enable your development team to see peek stats in production.

Better Errors

The Rails error page is quite bland by default and doesn’t give you much help in the way of finding the line of code that error'ed out. This is more true for Rails 4.0 and older. The better errors gem as it name suggests adds more information and generally makes the rails error page more useful for developers.

By default it shows you the line of code that caused the error in the browser itself. Second it makes the stack trace interactive. Clicking on any cell with give a preview on right right side. It also makes debugging faster by adding a live terminal below the code snippet where you can type in variable names and such and interact with the code in execution.

Below the that there is also a number of other useful information such as request header information and local variables and the values set with it. One thing to note is that you will notice a number of variables that are present but not created by you. Do not get alarmed, they’re normal and created by Rails.

Pry Rails

I cant even say the number of hours I’ve saved in debugging time by using this gem. Think of this as a debugger on steroids. Install it by doing putting gem "pry-rails", group:"development" into your gem file and bundling. Then type binding.pry in any controller and make rails hit that controller by visiting the page from your browser.

Class SessionsController
  def my_action
    @user = User.first
    binding.pry
  end
end

Now you should see the magic happen in your terminal running the rails server. The terminal should display something in the screenshot above. Pry has basically halted the execution at the point where you typed binding.pry and invoked that point in code in the terminal. You can type in the name of variable and see its value, you can even change the values of variables and basically do anything that you would do in a rails console. You can also move the code step by step by typing next. Exit the session and make it continue the execution as normal by typing exit. Another thing to remember is that you can use binding.pry anywhere in your code including views and models. To use it in a view simply put it inside the erb tags <% binding.pry %> and it will open a binding when the view is rendered.

Quiet Assets

Do your rails server log look like this? Filled with asset requests that you don’t really need to see, making you scroll endlessly to find the last request? My biggest annoyance would be that while scrolling I would more often than not miss the relevant request.

Quiet Assets as it names suggests removes all the asset requests from your rails console so you can see just the relevant request data. Installation is as simple as dropping gem 'quiet_assets', group: :development into your gemfile and bundling. You should see something to the screenshot below.

Bullet

N+1 queries are one of the most common culprits for slow pages in Rails apps. Bullet helps you find N+1 queries by showing alert every time it finds you doing an N+1 query in your Rails application. It will not work for applications that are using Rails as an API

NewRelic (No paid account required)

Yes this is Newrelic.com and you don’t need a paid account to access their local development features. This is probably one of the least known gems in all of the ones that I’ve recommended.

  # Place this at the bottom of the Gemfile
  # as recommened by Newrelic docs
  gem 'newrelic_rpm'

start by placing the newrelic_rpm at the bottom of your gemfile and bundling. Signup for a newrelic account which is free by going to Newrelic.com and get Newrelic.yml file and place it into your config folder of your rails application. I’ve found getting Newrelic developer mode is not the easiest and takes a bit of trail and error. So its best to checkout their official documentation.

However once you do get it setup, the metrics that it provides are quite useful. You can view it by visiting localhost:3000/newrelic. It gives you a breakdown of how long each query took to execute and also gives you a breakup of each part of your stack. Though I’m not running this all the time, I’ve found this to be extremely useful when trying to debug pages that are slow. By looking at their breakdowns you will know in a jiffy which part of the code to attack first and refactor for the biggest gains. There is a nice screenshot at the end of their official documentation page that shows what I mean.

Conclusion

Rails out of the box is pretty well configured, but these gems make working with it just that much better. It usually takes an extra 30 min in the beginning to get these gems set up correctly. However I’ve found all of that setup time to be recovered within the first few hours of programming, reclaimed almost exclusively from debugging time.

Are there gems that you use are not commonly known? I would love to hear about them, leave a comment below to share it with everyone.

comments powered by Disqus