What’s new in Rails 5

Rails 5 is out now. Current version of rails 5 – 5.1.0.rc1 was released on March 20, 2017. Without any doubts it is the best version yet. So do you want to know what are the new features of rails 5? Let’s see some of the star features or we can say major changes of it.

Major Changes

  • Action Cable

    Action Cable is the most talked about feature in Rails 5. This will make it super easy to add real time features to your app. It is a brand-new framework for handling WebSockets in Rails. It is completely integrated solution for managing connections, a server-side Ruby framework, and a client-side JavaScript framework. You have access to your full domain model written with ActiveRecord. It’s incredibly easy to use and makes designing live features like chat, notifications so much easier.

    In development mode, Action Cable runs in-process with the rest of your app. So, the default development server is changed to Puma from Webrick.

  • API Mode

    Rails is not a great choice only when you want to build a full-stack application that uses server-side rendering of HTML templates, but a great companion for the new crop of client-side JavaScript or native applications that just needs the backend to speak JSON. It’s even more clear with the new -api mode.

    The command for creating a Rails API is :

       rails new new_app –api

    This will create a stripped-down Rails application with a smaller set of middleware. It will change the parent for ApplicationController from ActionController:Base to ActionController::API.

    The following line is added to the config/application.rb file.

    module NewApp

       class Application < Rails::Application

           Config.api_only = true

       end

    end

    After these, you won’t have views, helpers and assets. But you can use JBuilder, Active Model Serializers.

  • Attributes API

    Attributes API is one of the major additions to the Rails 5. It allows you to define custom attributes on a model or override existing attributes. For example, you can create a MoneyType attribute type, and define how it gets stored as a price_in_cents field in the database.

    In file app/models/product.rb:

    class Product < ApplicationRecord
       attribute :price_in_cents, MoneyType.new
    end
    class MoneyType < ActiveRecord::Type::Integer
       def type_cast(value)
            # convert values like ‘$10.00’ to 1000
       end
    end
    product = Product.new(price_in_cents: ‘$10.00’)
    product.price_in_cents #=> 1000

  • Turbolinks 3

    Traditional web apps may take more time due to full page reloads. Rails 5 came up with its solution by just reloading the bare minimum content area. Turbolinks doesn’t refresh the entire page. Turbolinks 3 allows you to retain most of your page and selectively update certain regions through partials.

These are some major changes Rails 5 introduced. Few of them might have been missed by me but I tried to cover up most of them. Try something new with Rails 5 in your next project!