5 Best Ruby on Rails Gems for 2023 - Learn Enough | Learn Enough to Be Dangerous
You have to make a choice. Choose...wisely.

Get occasional notifications about things like product discounts, blog posts, and new or updated tutorials. Unsubscribe at any time.

Gift Delivery Options
Quick Checkout
or Pay by Credit Card
Error processing your payment
  • You didn't choose whether or not to be added to the mailing list
Confirm
$0.00

Payments and credit card details are securely managed and protected by Learn Enough's payment processor, Stripe. More information on their site:

CART
Total
$0.00

Your Cart is Empty

$30
$300
$300
$XY
$XY
1234
Top 5 Game-Changing Ruby on Rails Gems in 2023 to Know About

Top 5 Game-Changing Ruby on Rails Gems in 2023 to Know About

Ruby on Rails is a popular web development framework that has been around for more than a decade. It provides developers with a solid foundation to build web applications quickly and efficiently. One of the key benefits of using Rails is its vast ecosystem of Ruby gems.

What are gems in Ruby on Rails?

Ruby gems are small packages of reusable code that Rails developers can easily add to their projects to extend functionality. They are essentially plug-ins that can be installed into your Rails application to add specific features or functionality that are not included in the core Rails framework. A Ruby gem can range from small utilities that provide basic functionality such as authentication, image processing or file uploading, to more complex libraries that enable integration with third-party services such as payment gateways, messaging services, or social media platforms.

In this article, we'll take a look at the 5 of the best Rails gems for 2023. We'll discuss why they are popular, how to install them, and how to use them in your projects.

Quick note: The following example code is using Ruby on Rails version '~> 7.0.4' and Ruby '3.1.3'.

1. Devise — best authentication solution 

One famous Ruby gem is Devise, a flexible authentication solution for Rails web applications. It provides a customizable set of modules that can be used to add authentication to your application. Devise is highly configurable and allows developers to add their own authentication mechanisms, such as two-factor authentication or social media authentication.

Why is Devise popular?

Devise is a popular gem because it provides developers with a complete solution for user authentication, registration, and login. It's easy to install and configure, and it comes with a lot of features out of the box. Devise also integrates well with other Ruby on Rails gems and provides a lot of customization options.

Example use case of Devise

Suppose you're building an e-commerce website that requires users to sign up and log in before purchasing products. In that case, you can use Devise to handle user authentication, registration, and login. Devise provides you with a complete solution for user management, so you don't have to write complex authentication and registration code from scratch.

How to install Devise

To install Devise, add the following line to your Gemfile:

gem 'devise'

Then run this in your terminal:

$ bundle install

To use Devise in your Rails application, you need to run the following generator:

$ rails generate devise:install

This command generates a configuration file for Devise and sets up some initial configuration for your application.

How to use Devise

After installing Devise, you need to generate a model. For this example, we will use User Model by running the following command:

$ rails generate devise User

This command will generate a user model with email and password fields. You can then run the database migration command:

$ rails db:migrate

After successfully installing and integrating Devise, you can try to access Devise through your application using `/users/sign_in` path

devise-signin

or `/users/sign_up` to register the user first.

devise-signup

2. Paperclip — best uploader for Rails apps

Paperclip is a gem that makes it easy to upload files to your Rails application. It provides a simple API for handling file uploads and integrates with a number of popular storage solutions, such as Amazon S3 and Rackspace Cloud Files.

Why is Paperclip popular?

Paperclip is a popular gem because it simplifies the process of attaching files to Ruby on Rails models. It provides developers with an easy-to-use interface for file uploading and management. Paperclip also integrates well with cloud storage services, making it easy to store and retrieve files from remote servers.

Example use case of Paperclip

Suppose you're building a social media platform that allows users to upload and share images. In that case, you can use Paperclip to attach images to Post models. Paperclip makes it easy to upload and manage images, and it integrates well with cloud storage services, making it easy to store and retrieve images from remote servers.

How to install Paperclip

To install Paperclip, add the following line to your Gemfile:

gem 'paperclip'

Then, run the bundle command to install the gem.

$ bundle install

Note: If you encounter an issue while installing the gem, make sure you have already installed the `mimemagic` gem. and `imagemagick` library in your local machine. But if that hasn’t resolved the issue, try to install `shared-mime-info` first.

How to use Paperclip

To use Paperclip, you need to add a file attachment field to your model. For example, to add an image attachment to a Post model, you can add the following line to your model:

class Post < ActiveRecord::Base

  has_attached_file :image

end

Assuming you have a posts table and register PostsCRUD process, you also need to add the following lines to add a column to your model. Or you can directly use `rails generate paperclip post image`

class AddAttachmentImageToPosts < ActiveRecord::Migration[7.0]

  def up

    add_attachment :posts, :image

  end

  def down

    remove_attachment :posts, :image

  end

end

To upload a file, you can use the following code in your view:

<%= form_for @post, url: posts_path, html: { multipart: true } do |form| %>

  <%= form.label "Post image", style: "display: block" %>

  <%= form.file_field :image %>

  </br>

  <%= form.submit %>

<% end %>

Add a handler to your controller using the following code:

class PostsController < ApplicationController

 def create

  @post = Post.create(post_params)

 end

 private

# Use strong_parameters for attribute whitelisting

# Be sure to update your create() and update() controller methods.

 def post_params

  params.require(:post).permit(:image)

 end

end

And to show the image file, you can use the following code in your view:

<%= image_tag @post.image.url %>

<%= image_tag @post.image.url(:medium) %>

<%= image_tag @post.image.url(:thumb) %>

Here is an example view of the uploaded file using the above code:

paperclip-example

3. CanCanCan — best for defining permissions 

CanCanCan is a gem that provides an easy way to authorize actions in your Rails application. It allows you to define abilities for different roles in your application, and then check whether a user is authorized to perform a certain action.

Why is CanCanCan popular?

CanCanCan is a popular gem because it simplifies the process of defining and managing user permissions and roles. It provides developers with an easy-to-use interface for authorization, making it simple to control what users can and cannot do within an application.

Example use case of CanCanCan

Suppose you're building a blogging platform that allows users to create and manage their posts. In that case, you can use CanCanCan to define user roles and permissions. You can define an "admin" role that has full access to all posts, a "user" role that can only manage their own posts, and a "guest" role that cannot manage any posts.

How to install CanCanCan

To install CanCanCan, add the following line to your Gemfile:

gem 'cancancan'

Then run:

$ bundle install

How to use CanCanCan

To use CanCanCan, you need to define abilities for different user roles. For example, to define an ability for a user with the role "admin" to manage all posts. If the role is not an admin and you want it to only read the posts, you can add the following code to your Ability model:

class Ability

  include CanCan::Ability

  def initialize(user)

    if user.admin?

      can :manage, :post

    else

      can :read, :post

    end

  end

end

This code defines an ability for an admin user to manage all posts in the application, and for other users to only be able to read posts.

To check whether a user is authorized to perform a certain action, you can use the following code in your controllers:

def show

  @post = Post.find(params[:id])

  authorize! :read, @post

end

This code checks whether the current user is authorized to read the post before displaying it.

Also you can then use the can? method in your controller to check if a user has the necessary permissions to perform an action. For example, to check if a user can manage a post, you can use the following code:

if can?(:manage, @post)

  # User has permission to manage the post

else

  # User does not have permission to manage the post

end

4. ActiveAdmin — best administrative interface 

ActiveAdmin is a gem that provides a simple way to create an administrative interface for your Rails application. It allows you to quickly create CRUD interfaces for your models, as well as custom pages and reports.

Why is ActiveAdmin popular?

ActiveAdmin is a popular gem because it simplifies the process of creating administration interfaces for Ruby on Rails applications. It provides developers with an easy-to-use interface for managing application data and resources. ActiveAdmin also comes with a lot of built-in functionality and customization options.

Example use case of ActiveAdmin

Suppose you're building a content management system that allows users to create and manage blog posts. In that case, you can use ActiveAdmin to create an administration interface for managing posts, categories, and tags. ActiveAdmin provides you with an easy way to manage application data and resources without having to write complex code from scratch.

How to install ActiveAdmin

To install ActiveAdmin, add the following line to your Gemfile:

gem 'activeadmin'

Then run:

$ bundle install

To use ActiveAdmin in your Rails application, you need to generate the necessary files:

$ rails generate active_admin:install

This command generates the necessary configuration and initializer files for ActiveAdmin. It also creates an initial admin user that you can use to log in to the administrative interface.

Note: If you failed to install ActiveAdmin, make sure you have `sass-rails` gem in your gemfile because ActiveAdmin is dependent on that gem.

How to use ActiveAdmin

activeadmin

To use ActiveAdmin, you need to define resources for your application's models. For example, to define a resource for a Post model, you can add the following code to your admin/posts.rb file or use generator `rails generate active_admin:resource Post`:

ActiveAdmin.register Post do

  permit_params :title, :body

  index do

    selectable_column

    id_column

    column :title

    column :body

    column :image

    column :created_at

    actions

  end

end

This code defines a Post resource and specifies the permitted parameters for the resource. You can then access the administration interface for the Post resource at /admin/posts.

activeadmin-posts

5. Sidekiq — best for background processing

Sidekiq is a popular gem that provides a simple way to perform background processing in your Rails application. It uses Redis to store jobs and provides a web interface for managing and monitoring the job queue.

Why is Sidekiq popular?

Sidekiq is a popular gem because it provides a simple and efficient way to perform background jobs in Ruby on Rails applications. It allows developers to offload long-running or resource-intensive tasks from the main application thread, resulting in faster application performance and a better user experience.

Example use case of Sidekiq

Suppose you're building an e-commerce platform that sends order confirmation emails to customers. You can use Sidekiq to perform the email sending task in the background. When a customer places an order, you can enqueue a job to send the confirmation email to the customer's email address using Sidekiq. This ensures that the email sending task doesn't block the main application thread and results in a faster and smoother user experience.

How to install Sidekiq

To install Sidekiq, add the following line to your Gemfile:

gem 'sidekiq'

Then run:

$ bundle install

How to use Sidekiq

To use Sidekiq, you need to define a worker class for your background job. For example, to define a worker class for sending an email, you can add the following code to your app/workers/email_worker.rb file:

class EmailWorker

  include Sidekiq::Worker

  def perform(user_id)

    user = User.find(user_id)

    # Send email to user

  end

end

This code defines an EmailWorker class that includes the Sidekiq::Worker module. The perform method of the worker class is called when the job is executed. You can then enqueue a job to be executed in the background using the following code:

EmailWorker.perform_async(user.id)

This code enqueues a job to be executed in the background, passing the user ID as a parameter.

To view the Sidekiq web interface, you can navigate to /sidekiq in your application's URL. This interface allows you to view and manage the job queue, as well as view statistics and logs for your Sidekiq instance.

sidekiq

In closing

In this article, we've explored 5 of the best Ruby on Rails gems for 2023. We hope this article has been helpful in introducing you to some of the most popular gems in the Ruby on Rails ecosystem. By incorporating these gems into your Rails applications, you can improve functionality, add authentication, and streamline your Rails development process. As always, it's important to thoroughly research and test any new gems before using them to develop custom Rails apps.

In addition to the gems mentioned in this article, there are many other excellent gems available for Ruby on Rails, and the Rails community is continuously creating and updating gems to improve the functionality and ease of development for Rails developers. We encourage you to explore theRubyGems website andGitHub to discover new gems for Rails web apps and find solutions to your specific web application development needs.

Remember, gems are just one tool in your development toolkit. It's important to have a solid understanding of the underlying technologies and principles behind them in order to use them effectively.

Thank you for reading, and happy coding!


About Learn Enough

At Learn Enough, we provide a carefully designed course to take you from a beginner to a professional-grade developer. Every Learn Enough All Access subscription includes theRuby on Rails Tutorial, the leading introduction to web development with Rails for over ten years. If you manage a team of developers, Learn Enough for Teams boosts the skills of your junior devs and gets your senior devs quickly up to speed with the latest version of Ruby on Rails. 

Start your all-access 7-day free trial today!

Popular Posts

Rails Hotwire, Turbo & Stimulus: Best Webpacker Alternatives

CMD Meaning and Basic Commands for Beginners

Best Ruby on Rails Course: Top 8 in 2023

Migrating Away from Webpacker: A Guide to Upgrading Your Rails 6 to Rails 7

Join the Mailing List

Get occasional notifications about things like product discounts, blog posts, and new or updated tutorials. Unsubscribe at any time.