Beginner

How to upgrade from rails 4.2.9 to rails 5.1 and don’t die trying


Hi there 👋

 

I’m going to show you how I upgraded Rails app from version 4.2.9 to Rails 5.1.

The recommended way of upgrading your Rails application is doing it version by version, for example, in this case, we would begin upgrading from Rails 4.2.0 to Rails 5.0.0.0 and then 5.0.0.1 and so on, here I’ll show you most of the problems I’ve noticed during the upgrading and how I managed to fix them.

If you know a better way how to deal with them I’d like to read it in the comments, and anyway I hope these tips work for you.

Issues log

– The deprecated and removed filter callbacks. In Rails 5.0 we got a warning of the deprecation of all the filter callbacks and in Rails 5.1 they have been removed and replaced by the action callbacks, this is an easy fix for example:

#in rails 4.2.9
before_filter :require_login
#in rails 5.1
before_action :require_login

This is just a syntax change because the action callbacks do the same as the filter callbacks and this change was made because the “action” word makes reference to the seven CRUD actions: index, new, create, show, update, edit, & destroy that Rails manage; this is how the before_action executes that callback before the CRUD action is performed.

– Unsorted params in test environment, for example:


# what I send post '/', params: { "items" => [ { "brand" => { "name" => "Apple" }, "id" =>1, "color" => "pink" }, { "brand" => { "name" => "Samsung" }, "id" => 2, "color" => "gold" }, ] } # what appears in controller {"items"=>[{"brand"=>{"name"=>"Samsung"}, "id"=>"1", "color"=>"pink"}, {"id"=>"2", "color"=>"gold"}]}

 

This error seems to happen just in testing environment, my personal workaround for this is to send the params as json, the past example and more info can be found here: https://github.com/rails/rails/issues/23997

– The migrations.

From Rails 5.1 migrations don’t inherit from ActiveRecord::Migration anymore. We need to specify for which rails version the migration was created for; this applies only to those migrations that have been created before this upgrade. The generator of Rails 5.1 will add the version automatically to the ones created for now on for example:

 


# in rails 4.2.9 class MyMigration < ActiveRecord::Migration #in rails 5.1 class MyMigration < ActiveRecord::Migration[4.2]

 

This change was applied because the rails 5 API migration had changed, therefore adding the Rails version to the migration was implemented to improve the API process, because previous migrations like 4.2 and prior create different schemas than the new ones, more info about this can be found here: https://blog.bigbinary.com/2016/03/01/migrations-are-versioned-in-rails-5.html

– Add the params key to the controllers’ tests.

This wasn’t required in previous Rails versions but in Rails 5 it became required, for example:

 


# rails 4.2.9 post '/', { "items" = [ { "brand" = { "name" = "Apple" }, "id" =1, "color" = "pink" }, { "brand" = { "name" = "Samsung" }, "id" = 2, "color" = "gold" }, ] } # rails 5.1 post '/', params: { "items" = [ { "brand" = { "name" = "Apple" }, "id" =1, "color" = "pink" }, { "brand" = { "name" = "Samsung" }, "id" = 2, "color" = "gold" }, ] }

 

This change was implemented because we can send more params to some helpers and sometimes it became confusing for example:

 


class ProductsControllerTest < ActionController::TestCase <span class="k">def</span> <span class="nf">test_show</span> <span class="n">get</span> <span class="ss">:show</span><span class="p">,</span> <span class="p">{</span> <span class="ss">id: </span><span class="n">user</span><span class="p">.</span><span class="nf">id</span> <span class="p">},</span> <span class="p">{</span> <span class="ss">notice: </span><span class="s1">'Welcome'</span> <span class="p">},</span> <span class="p">{</span> <span class="ss">admin: </span><span class="n">user</span><span class="p">.</span><span class="nf">admin?</span> <span class="p">}</span> <span class="n">assert_response</span> <span class="ss">:success</span> <span class="k">end</span> <span class="k">end</span> # { id: user.id } this one are the params #{ notice: 'Welcome' } this is a flash message #{ admin: user.admin? } this is a session

Now Rails 5 forces us to use keys to indicate what param is what and this makes it more readable.

– View helpers stopped  working

At some point during the update all view helpers stopped working in my  angular 1 views (I use angular-rails-templates gem for angular views) this was kind of hard to debug, but easy to fix, the problem was in the configuration of angular rails templates, to fix this I just changed the following:

# app/config/initializers/angular_rails_templates.rb

# rails 4.2.9
.... omitted code
Tilt.register CustomHamlEngine::HamlTemplate, '.haml'
#rails 5.1
Tilt.register CustomHamlEngine::HamlTemplate, 'haml'

This error has been thrown because we updated the dependencies, therefore, those dependencies that require Tilt been updated did it from version 1.4.1 to 2.0.8.

The most important thing about updating a Rails app is to have a ton of patience, because the issues described here are just some of the few you may run into, some may be easy some others can take a little more, so don’t get frustrated if you get stuck sometimes on them, and if you have any doubt or improvement I’d be happy to hear about it.

Best Practices
De Código, Café y Cervezas 07 – ¿Somos profesionales?
Beginner
Importing fonts the right way
Development
What is VTEX platform and why should you migrate your ecommerce to it?