12 Useful Ruby Gems for Rails
Whether you are building a full ruby on rails application, or building a rails api to connect to a different front end more often than not you will likely make use of gems. Whether to add a new feature or to simplify a complex one, gems are a great way to add something to a project and save yourself a bit of code in the process. In this article I will be discussing 12 gems that can help you manage your databases, track and handle errors, set up authentication and authorization, and talk to a front end client that can be useful the next time you begin building out a project!
Database Management
Active Record Reset PK Sequence
This is a very simple gem that reverts the primary key of a model back to 1 when you want to reseed your database during testing. It adds a method ‘reset-pk-sequence’ to ActiveRecord which is then passed down to every model you create. When building out an application there may be times when you want to update or reseed your data. Depending on the size of the application reseeding the database can cause your primary keys to rapidly grow. The ability to reset your primary key’s is a great way to reset the information in your database without actually resetting the entire database.
Documentation:
https://github.com/splendeo/activerecord-reset-pk-sequence
Installation:
There are two ways to install this (and every gem here) first inside of the gemfile add:
gem ‘activerecord-reset-pk-sequence’
Then in your terminal run:
$ bundle install
-or-
In your terminal run:
$gem install activerecord-reset-pk-sequence
Rest-Client
Rest Client is a great gem for scraping external api’s to seed your database, or to make HTML requests within a rails application. For database seeding you would use a ‘GET’ request but rest-client also allows you to make ‘POST’, ‘PATCH’, ‘DELETE’, ‘PUT’ requests; which is great for calling on external API’s in a rails application.
Documentation:
https://github.com/rest-client/rest-client
Installation:
In gemfile:
gem ‘rest-client’
Then in terminal:
$ bundle install
-or-
$ gem install rest-client
Nokogiri
Occasionally external API’s don’t store their information in an easily readable manner. Some of the attributes may be written as HTML/XML/SAX. Using the nokogiri gem allows you to parse out the data you are seeking from those attributes and capture the data you are looking for.
Documentation:
https://github.com/sparklemotion/nokogiri
Installation:
In gemfile:
gem ‘nokogiri’
Then in terminal:
$ bundle install
-or-
$ gem install nokogiri
Faker
Faker is a great way to populate a database with information without having to create the seeds yourself. Faker provides a wide variety of generators ranging from users, to cars, to addresses, and more. This allows you to create a large variety of tests to get a feel for how your app could work with a larger database.
Documentation
https://github.com/faker-ruby/faker#default
Installation
gem ‘faker’ :git => ‘https://github.com/faker-ruby/faker#default’, branch => ‘master’
Then in your terminal run:
$ bundle install
-or-
In your terminal run:
$ gem install faker* note you may still need change your gemfile to match above using this method
Authentication/Authorization
Bcrypt
The Bcrypt gem serves as a way to add some security to your application. It provides a validation that creates an encrypted password and saves that to your database instead of the actual password being saved directly. As developers it is important that we try to protect our users information as best we can. For that reason this is one of the most important gems you could use since it adds some base level security to a user’s information.
Documentation:
https://github.com/codahale/bcrypt-ruby
Installation:
In gemfile uncoment:
# gem ‘bcrypt’
Then
$ bundle
-or-
In your terminal run:
$ gem install bcrypt
Ruby-JWT
This gem provides ruby/rails applications the ability to implement the RFC 7519 OAuth JSON Web Token standard. A JWT Token adds an extra layer of security to web apps in the authentication process. Rather than storing your user information in something like localStorage or cookies you simply store and then check their unique JWT token to allow them to access their information.
Documentation:
https://github.com/jwt/ruby-jwt
Installation:
In gemfile:
gem ‘jwt’
Then
$ bundle
-or-
In your terminal run:
$ gem install jwt
Dotenv-Rails
If you are not planning on creating an open source project you will need to set up environment variables so that only you and your team can run the application. This gem helps you to easily set up environment variables while you are still in development. It allows you to create secret keys for your application that won’t be posted to github once you add it to your .gitignore file.
Documentation:
https://github.com/bkeepers/dotenv
Installation:
In gemfile:
gem ‘dotenv-rails’
Then
$ bundle
-or-
In your terminal run:
gem install dotenv-rails
Devise
The devise gem provides an authentication framework to rails based applications. It adds a lot of different functionalities for creating, authenticating, and authorizing users. Such as, the ability to lock an account, to validate them, recover passwords, send email confirmations, add omniAuth and more.
Documentation:
https://github.com/heartcombo/devise
Installation:
In gemfile:
gem ‘devise’
Then
$ bundle
-or-
In your terminal run:
$ gem install devise
Cross Origin Resource Sharing
Rack-Cors
This gem is extremely important if you are going to connect a rails api to a different client. This gem provides support for Cross-Origin Resource Sharing. Once it is installed you can configure the cors file to speak to multiple different clients or one client in particular.
Documentation:
https://github.com/cyu/rack-cors
Installation:
In gemfile uncoment:
# gem ‘rack-cors’
Then
$ bundle
-or-
In your terminal run:
$ gem install rack-cors
Testing/Error Handling
Byebug
The byebug gem is a great way to trace errors and build out methods step by step in an application. Adding a byebug to a method in your application allows you to enter an irb session when running that code from your localhost.
Documentation
https://github.com/deivid-rodriguez/byebug
Installation:
Out of all these gem’s byebug should already be set up when you create a rails application. However should you need to install it simply follow these familiar steps:
In gemfile:
gem ‘byebug’
Then
$ bundle
-or-
In your terminal run:
$ gem install byebug
Better Errors
While ruby itself provides wonderful error support, if you still find yourself struggling to understand them this gem helps by rendering a plain text error page laying out your errors for you.
Documentation:
https://github.com/BetterErrors/better_errors
Installation:
In gemfile:
gem ‘better_errors’, ‘~> 2.1’, ‘>= 2.1.1’
Then
$ bundle
-or-
In your terminal run:
$ gem install better_errors -v 2.1.1
Rspec-Rails
There are many different testing frameworks available for Rails applications. Rspec is among one of the more popular ones. Rspec allows you to create tests that provide detailed explanations of how an application is supposed to behave when it is running. If you are building a project on your own it may seem simpler to test by trial and error. But once you are working with a larger team on a larger application running tests before integrating any new code will help to ensure that adding a new feature, or fix will not break your application.
Documentation:
https://github.com/rspec/rspec-rails
Installation:
In gemfile:
gem ‘rspec-rails’, ‘~> 4.0.0’
Then
$ bundle
-or-
In your terminal run:
$ gem install rspec-rails
Conclusion
These are just 12 gems that can really come in handy when developing any ruby on rails application. Definitely consider adding them then next time you start a project! Happy Coding!