Article

Top 5 Ruby Gems to use in your Rails application


Ruby gems that will enhance productivity as well as provide better functionality and design to your rails application. Believe me, after going through these gems, you will realize how awesome rails framework can be.


1. Bootstrap-Generators


gem 'bootstrap-generators'

Twitter bootstrap is undoubtedly the most popular front-end framework to use for your website. It can easily give a smooth and professional look to any amateur looking website. It is easy to use too once your learn the syntax and conventions.

Once included in your gemfile, you need to modify your rails app in order to include new CSS and Javascript files of bootstrap, as well as changing the views and application layout to adopt Twitter bootstrap style. To do this, a generator is included in this gem. Run the following command in your rails directory to ‘bootstrapify’ your rails app :

rails generate bootstrap:install -f

This will overwrite your application template to bootstrap style. It will also modify view generator templates to include bootstrap elements in scaffoldings’ views. For e.g. :

rails g scaffold User name:string email:string age:integer

Now go to http://localhost:3000/users. You will notice a clean, smooth, bootstrapped layout.


2. Better_Errors


group :development do

gem 'better_errors'

gem 'binding of caller'

end

This is a must have gem if you work on rails. It replaces all the error pages that you encounter when browsing website in production mode. The new error pages are more detailed, have a cleaner interface and provides you with a working web console! Couldn’t have been more awesome.

Following are some of its features :

1. Call Stack trace

2. Shows data in the instance variables

3. Ruby Console have scope of the error block


3. Devise


gem 'devise'

Devise gem makes adding authentication system to your rails app, a cakewalk. Adapting the ‘convention over configuration’ philosophy of rails, the devise gem has got all the configurations of a typical authentication system ready for you. It provides you with quick starting generators, helpers and an easy to use API to create a secure login system for your app. For quickly creating a user login system, follow these commands:

rails generate devise:install

rails generate devise User

rake db:migrate

After that, restart your rails server. A basic user login system with default parameters is installed. Navigate to http://localhost:3000/users/sign_up to open the user sign up page. To sign in, open http://localhost:3000/users/sign_in. But still this authentication does not protect any controller or action. To add user authentication to any controller, add the following filter at the start of a controller.

before_filter :authenticate_user!

To interact with the functionality of devise, some helpers are provided. Following method demonstrates the usage of these helpers:

def check_user

if user_signed_in?

u = User.find(current_user.id)

@email = u.email

user_session[:token] = generate_hash(u.email)

else

@notice = "Please log-in first"

end

end

Further information on this gem can be found here.


4. Will Paginate


gem 'will_paginate'

This gem will add pagination to any model of your rails app in a few lines of code. This gem also provides view helpers that renders navigation links to different pages in HTML. You can then add CSS styling to these rendered links to match them with the styling of your website or to prettify them according to your needs.

For example, if you want to add pagination to your posts model, replace the active record statement in your controller with the following

 @posts = Post.all.paginate(:page => params[:page], :per_page => 10)

And in your view, add the following to place the HTML code of page links

 <%= will_paginate @posts %>


5. Thinking Sphinx


gem 'thinking-sphinx'

If your database is very large, performing even a simple text search can take a lot of time. Here, Sphinx comes to the rescue. To reduce search query timing, Sphinx performs indexing on the database, and serves this indexed database as a daemon on a network port (like MySql servers on port 3306). Thinking Sphinx gem provides an Active Record interface for this new indexed database, served by a Sphinx daemon.

I will be writing a separate tutorial on Sphinx soon. For more information about its usage, visit this link.

Rohan Raja

Recently graduated, majoring in Mathematics and Computing from IIT Kharagpur, Rohan is a technology enthusiast and passionate programmer. Likes to apply Mathematics and Artificial Intelligence to devise creative solutions to common problems.