What's new in Rails 5.1?

04 Mar 2017

It’s time for another version of Rails.
The first beta of Rails 5.1 was released a few days ago,
and it introduces some goodies that will make things a lot easier
for people who need to work with JS in their Rails apps.
Here’s a quick tour of the new features:

Yarn is part of the default stack

In my previous post, I wrote about
using yarn with Rails.
With Rails 5.1, you no longer have to do anything -
it will be supported out of the box.
Yarn is the equivalent of Bundler for JS libraries,
and this will make it a lot easier to manage those dependencies.

Webpack option

Rails’ official webpacker gem
now makes it easy to integrate webpack with Rails apps.
Rails has a --webpack option to add this gem to Gemfile,
and set up the project to work with Webpack.

Easily add React, Angular or Vue.js

With the introduction of webpacker,
you also have to option of easily setting up
React, Angular or Vue.js as part of our Rails project.
Webpacker takes care installing the dependencies,
and creating the configuration files.

System tests with Capybara

Rails now has ActionDispatch::IntegrationTest that lets you
use Capybara for integration tests, without needing any extra configuration.
By default, it uses Chrome browser to run the tests,
but can be configured to use Poltergeist, Selenium or Firefox.
Here’s an example taken from Rails docs:

Encrypted secrets

The recommended way to store secrets, such as passwords and secret tokens,
was to put them in environment variables.
Now there is an option to set up an encrypted secrets file
which can be checked into source control.
You will have a master key, stored outside the repo,
which can be used to decrypt the keys.

BIGINT primary keys

If you’re using Postgres or MySQL, primary keys are BIGINT by default.
This is not something you need to worry about unless you have tables
that are likely to have more that 2**31 - 1 rows in a table,
but making this the default will make this one less thing to worry about.
Jon McCartie, who added this feature,
has written in detail about this.

New form_with helper

A new form_with
helper has been added which unifies the form_for and form_tag methods.
Examples:

New tag helper

Goodbye, jquery

Rails UJS no longer depends on jquery,
so jquery-rails will no longer be included by default.
You might still need jquery in your app, but now you can add it via Yarn:

bin/yarn add jquery

Upgrading

For a minor release version, Rails 5.1 has quite a few interesting changes,
but despite that upgrading from Rails 5.0 shouldn’t be too difficult.
I tried upgrading a small project and was up and running in 10 minutes or so.

Links

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.