Ruby on Rails Gems for Web Development
I have been programming on Ruby on Rails for many years and have solved a variety of rather complex tasks using this excellent framework. Based on my experience, I put together a short list of gems which I consider to be the most useful. In this article, I would like to share this list and show you how to find the helpful gems for RoR.
To get in-depth knowledge on Ruby, enroll for live free demo on Ruby On Rails Online Training
Unfortunately, the format of gemís specification does not include defining categories and tags. Thatís why, when searching for gems to do certain tasks, we may only hope that the author of the gem has mentioned some keywords in the description. You can find many gems on rubygems.org or github.com. The searching can be done by description (on GitHub, you also have to select Ruby from the list of languages).
Another thing worth being mentioned is the Ruby Toolbox. It allows searching for gems by categories and popularity. But do not rely only on this source because the author of Ruby Toolbox adds new gems manually.
Foreigner
This gem helps to create foreign keys for tables. It is very easy to use. You may simply put it to Gemfile and it will add two new methods to your migrations: add_foreign_key
and remove_foreign_key
. Also, you will be able to add/delete keys right from create_table
and change_table
with foreign_key
and remove_foreign_key
methods.
Letís imagine that we need to add a key from the comment table to the posts table. We can do it the next way:
class CreateComments < ActiveRecord::Migrationdef changecreate_table :comments do |t|# … t.references :post# ...t.foreign_key :postsend# …endend
These methods have some extra options, e.g. name, column, dependent. You can read more about them in documentation.
Someone may say that it is not actual for new versions of Rails, but itís only starting from version 4.2 (where such functionality is already available out of the box). In all other cases, in my opinion, this gem deserves to be in the list of useful ones.
letter_opener
A simple but very helpful gem. In fact, it is a plug for saving emails in files instead of sending them. To activate this gem, you have to set letter_opener
as the delivery method in the appís configuration (for example in config/enviroments/development.rb).
config.action_mailer.delivery_method = :letter_opener
Bingo! Now all outgoing messages will be stored in /tmp/letter_opener folder and new emails will be previewed in browser right after sending. It is easy and practical.
Kaminari
This gem allows to easily create paginators of any complexity. Kaminari supports several ORMs (ActiveRecord, Mongoid, MongoMapper) and template engines (ERB, Haml, Slim).
Kaminari does not embed in basic classes: array, hash, Object and ActiveRecord::Base.
To start using Kaminari, it is enough to put it in Gemfile. After that some functions will become available, e.g. page, per and padding. Now you can effortlessly convert your array into a paginatable one with the help of Kaminari.paginate_array
method and then lots of useful pagination features will become accessible.
@paginatable_array = Kaminari.paginate_array(my_array_object).page(params[:page]).per(10)
The default configuration can be made in Kaminari.configure
initializer. default_per_page
, max_per_page, max_pages
- this is just a brief list of options which can be set.
Besides, the pagination can be configured individually for each model:
If you need to customize the paginator, you can create templates by running a generator:
% rails g kaminari:views default # -e haml - if you want to use HAML template engine.
The templates will be created in app/views/kaminari/ and now you can easily edit them.
Localization (I18n) and labels, themes and friendly urls, as well as other useful options can be found in the documentation for this gem. For more Additional info at Ruby On Rails Course
CarrierWave
With CarrierWave you are able to upload any files from your RoR application. All you need to do is:
Create an uploader:
rails generate uploader ProductPhotoUploader