What's new in Rails 5.1?
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.
Declarative exception handling in ActiveJob
ActiveJob now has a cleaner API for handling retries.
Here are some of the
examples for retry_on
from the docs:
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:
Parameterized mailers
ActionMailer::Parameterized
provides a way to perform setup with before_action
,
by passing in parameters to the method.
New tag helper
The ActionView tag
helper has been changed to provide a cleaner syntax
(see docs).
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:
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.
Integrating front end libs has always been a pain in Rails, since there was not standard way to do it. So making Yarn and Webpack first class citizens of the Rails stack in awesome news. Capybara too is something I found myself configuring all the time in Rails projects, and not having to do that is another win.