What's new in Rails 5?

04 Jan 2016

Rails 5.0 is almost here!
The first beta version has been released already,
and you can try out some of the shiny new features right away.
So what are the new features?
Let’s take a quick tour of the new features and major changes.

ActionCable

Websocket support in Rails is the most talked about feature in Rails 5.
This will make it super easy to add realtime features to your app.
If you’re planning to explore ActionCable,
here’s a couple of getting started guides:

Attributes API

A new attributes API has been introduced that allows you to define
custom attributes on a model or override existing attributes.
For instance, you can create a MoneyType attribute type,
and define how this gets stored as a price_in_cents field in the database.

It also allows you to override types of existing database attributes
(eg. you were using a decimal field in the database,
and now only need the values as integers),
or even to define non-persisted attributes on a model.
Read more about the attributes API here:

ApplicationRecord

And speaking of ActiveRecord, you might have noticed
that the Product model above inherits from ApplicationRecord,
not ActiveRecord::Base.
This is because Rails now generates that class
so that we can add all our customizations there
instead of mokeypatching ActiveRecord::Base.
Read more about it here:

Rails API

The rails-api gem has been merged into Rails.
It allows you to generate API-only Rails app
and strips out all the middleware that you don’t need
in an app that generates JSON responsed instead of HTML.
You can now generate an API-only app using the --api option:

rails new my-app-api --api

ActionController::Renderer

View rendering has been moved out into ActionController::Renderer
and is available for use anywhere via ApplicationController.render method.
This lets you render partials into strings and use them
in background jobs and other classes.
Here’s an example of how to render a partial using the render method:

If you’re wondering why you would want to render views outside controllers
you can see this in action in the code for the
Getting Started with ActionCable
guide, where it is used for rendering HTML into a string in background job
which is then sent to the clients via websockets.

Turbolinks 3

Update (19 Jan, 2016):From the looks of it, Turbolinks 3 will not ship with Rails 5.
Instead, a complete rewrite, Turbolinks 5, will be included
when the final version of Rails 5 ships.
(Follow the discussion here.)
I will update this section when more information is available about TL5.

Turbolinks 3 introduces partial replacement,
which allows you to replace only specific divs on a page.
Using data-turbolinks-permanent, you can also transfer DOM elements
from page to page along with the states.
This is useful for sections like navbars that do not change across pages.

On the server side, you can trigger partial replacement
using additional options with redirect_to or render methods.
For example, take a look at the following code:

If the comment is saved successfully in the above case,
only the div with id “comments” is replaced
with the matching element from the rendered HTML at comments_url.
In case of an error, only the ‘new_comment’ element, which is the comment form,
is replaced by the matching element from the response.

New command router

Ever accidentally typed rails db:migrate or rake generate model?
Figuring out which command to use is often confusing to beginners.
Now all such commands are routed through rails,
so we will be able to use commands like rails db:migrate.
The rake commands are still available if you want to use them.

Snappier development mode

In development mode, Rails reloads constants
when it finds that the file has changed.
Until now it was done by checking the mtimes for all the files on each request
and reloading if something has changed.
Rails 5 introduces an evented file system monitor
which asynchronously calls Rails when something changes.
As a result, it no longer needs to check all the files on each request,
and this makes the development mode much faster.
Read more about it here:

Test runner

The test runned for minitest has seen many improvements,
including improved error messages, color output
and the ability to run a single test by referring to the line.
This has finally brought the minitest
Read more about the improvements in the test runner here:

Supports only Ruby 2.2.2+

Rails has always pushed the community towards the latest Ruby versions,
and 5.0 is no different.
This version will only work on Ruby 2.2.2 and above.
This lets Rails to improve performance
by making use of the latest improvements in the Ruby garbage collector
(like incremental GC and symbol GC).

Upgrading

With the release of Rails 5, all versions of Rails from 4.1.x and below
will no longer be supported.
Bug fixes will only be released for Rails 5.0.x and 4.2.x.
Now would be a good time to upgrade,
at least to 4.2.x if you haven’t already done so.
If you’re planning to upgrade to Rails 5.0, these resources could be useful:

Links

A lot of new features have been introduced in Rails 5,
and I might have missed a few interesting ones.
Please do leave a comment pointing out any interesting features
I might have missed and I’ll add it to the list.

Hi, I’m Nithin Bekal.
I work at Shopify in Ottawa, Canada.
Previously, co-founder of
CrowdStudio.in and
WowMakers.
Ruby is my preferred programming language,
and the topic of most of my articles here,
but I'm also a big fan of Elixir.
Tweet to me at @nithinbekal.