Upgrade Rails from 5.0 to 5.1

This article is part of our Upgrade Rails series. To see more of them, click here.

This article will cover the most important aspects that you need to know to get your Ruby on Rails application from version 5.0 to 5.1.

1. Ruby version

Like Rails 5.0, Rails 5.1 requires Ruby 2.2.2 or later.

If you want to know more about the Ruby versions that you could use, check out our Ruby & Rails Compatibility Table.

2. Gems

Make sure the gems you use are compatible with Rails 5.1, you can check this using RailsBump.

If you can't find the gem in RailsBump, you could try using next_rails to find incompatibilities in your dependencies:

gem install next_rails bundle_report compatibility --rails-version=5.1.0

If that does not work, you'll need to manually check the GitHub page for the project to find out its status. In case you own the gem, you'll need to make sure it supports Rails 5.1 and if it doesn't, update it.

3. Config files

Rails includes the rails app:update task. You can use this task as a guideline as explained thoroughly in this post.

You might run into an error trying to run that command:

$ bundle exec rails app:update rails aborted! LoadError: cannot load such file -- rails/commands/server /Users/etagwerker/.rvm/gems/ruby-2.4.9@ombu/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:34:in `require' /Users/etagwerker/.rvm/gems/ruby-2.4.9@ombu/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:292:in `block in require' /Users/etagwerker/.rvm/gems/ruby-2.4.9@ombu/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:258:in `load_dependency' /Users/etagwerker/.rvm/gems/ruby-2.4.9@ombu/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:292:in `require' /Users/etagwerker/Projects/ombulabs/ombushop/config/application.rb:5:in `<main>'

If that is the case, you will need to change this require statement:

require 'rails/commands/server'

To:

require 'rails/commands/server/server_command'

As an alternative, check out RailsDiff, which provides an overview of the changes in a basic Rails app between 5.0.x and 5.1.x (or any other source/target versions). Always target your upgrade to the latest patch version (e.g: 5.1.6 instead of 5.1.0).

Some assets configuration changes you'll have to do on your config/environments/{development, test, production}.rb files are:

Before:

config . serve_static_files = false config . static_cache_control = "public, max-age=3600"

After:

config . public_file_server . enabled = false config . public_file_server . headers = { 'Cache-Control' => "public, max-age=3600" }

4. Application code

4.1. ActiveRecord The raise_in_transactional_callbacks option is now removed. It was already deprecated and covered in a previous upgrade.

Also removed was use_transactional_fixtures , which was replaced by use_transactional_tests .

ActiveRecord::Base#uniq was removed, it was deprecated in Rails 5.0 and has been replaced by #distinct . Check out https://github.com/rails/rails/pull/20198 for the discussion.

4.2. Controllers Before Rails 5.1, conditions in filters could be invoked using strings. They now have to be symbols:

Before

before_action :authenticate_user! , unless : 'has_project_guest_id'

After:

before_action :authenticate_user! , unless : :has_project_guest_id

All *_filter methods are now called *_action :

These methods were actually already deprecated in Rails 5.0, and Rails 5.1 removes support for *_filter usage, so you should be using *_action .

Before:

skip_before_filter :authenticate_user! before_filter :authenticate_user! after_filter :do_something

After:

skip_before_action :authenticate_user! before_action :authenticate_user! after_action :do_something

5. Testing

Parameters in controller tests now need a params key:

Rails 5.0 had already deprecated this behavior, and Rails 5.1 drops support for passing parameters without using keyword arguments. This change is necessary even if you're using RSpec:

Before:

expect { post :create , params } . to change ( Project , :count ) . by ( 1 )

After:

expect { post :create , params : params } . to change ( Project , :count ) . by ( 1 )

6. Next steps

If you successfully followed all of these steps, you should now be running Rails 5.1! Do you have any other useful tips or recommendations? Share them with us in the comments section.

If you're not on Rails 5.1 yet, we can help! Download our free eBook: The Complete Guide to Upgrade Rails.