Nithin Bekal

Posts About Notes Slides

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.

# For new projects
rails new foobar --webpack=react

# in existing projects
rails webpacker:install:react

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:

require 'application_system_test_case'

class Users::CreateTest < ApplicationSystemTestCase
  test "adding a new user" do
    visit users_path
    click_on 'New User'

    fill_in 'Name', with: 'Arya'
    click_on 'Create User'

    assert_text 'Arya'
  end
end

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:

class RemoteServiceJob < ActiveJob::Base
  retry_on CustomAppException # defaults to 3s wait, 5 attempts
  retry_on AnotherCustomAppException, wait: ->(executions) { executions * 2 }
  retry_on ActiveRecord::Deadlocked, wait: 5.seconds, attempts: 3
  retry_on Net::OpenTimeout, wait: :exponentially_longer, attempts: 10

  def perform(*args)
    # ...
  end
end

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:

# Instead of form_for
<%= form_with model: @post do |form| %>
  <%= form.text_field :title %>
<% end %>

# Instead of form_tag
<%= form_with scope: :post, url: posts_path do |form| %>
  <%= form.text_field :title %>
<% end %>

Parameterized mailers

ActionMailer::Parameterized provides a way to perform setup with before_action, by passing in parameters to the method.

InvitationMailer.with(invitee: foo).account_invitation.deliver_later

class InvitationMailer < ApplicationMailer
  before_action { @invitee = params[:invitee] }

  def account_invitation
    mail to: @invitee.email, # ...
  end
end

New tag helper

The ActionView tag helper has been changed to provide a cleaner syntax (see docs).

# Old
tag('div', class: 'foo')

# New
tag.div('Hello, world!', class: 'foo')
tag.div tag.p('Hello world!')

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.

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.

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.