[Ruby] [Rails] I tried increasing the version of Rails from 5.0 to 5.2

5 minute read

Thing you want to do

I want to upgrade the version from Rails 5.0 to Rails 5.2. (I wanted to use Active StoragewhichisavailablefromRails5.2.)

The purpose of this article is to introduce Required procedure for upgrading from Rails 5.0 series to Rails 5.2 series and Error handling at that time (and Active Storage ).

Environment

Ruby 2.5.1 Rails 5.0.7.2

Procedure

I greatly referred to Mr. Ito’s Qiita article. Permanent preservation version! ? Ito-san-shi/Rails app upgrade procedure

Basically, it’s easier to read Mr. Ito’s article (at the end of this book). However, I think that there are a few people who say “I want to upgrade the version as soon as possible!!”, so I hope you will see it in an evil way.

Originally, we should cover even the tests, but this time ** the purpose is to use Active Storage **, so I will omit about that.

1. Upgrade gems other than Rails

If you raise the version of Rails itself, surrounding gems such as Devise and Carrierwave may not support the latest Rails, and unexpected errors may occur.

Yes. It just happened. Lol

1-1. Update development and test group gems first

terminal


# Run in the corresponding app directory
$ bundle update -g development -g test

1-2. Update gems that are likely to cause trouble one by one

Running the bundle outdated command will list the gems that are out of date. However, if you don’t have that many gems, you might want to update all at once.

To update one by one, specify the gem name after bundle update.

terminal


When you want to update only # devise
$ bundle update devise

1-3. Update other gems at once

It is a bundle update without specifying the gem name. *Please do not change the version of Rails at this timing (leave the original version specified). Rails version upgrade will be done separately. **

Gemfile


# Keep Rails version specified
gem'rails','~> 5.0.7','>= 5.0.7.2'

After confirming that the Rails version is specified, bundle update

terminal


Update all gems together
$ bundle update

2. Raise the version of Rails

Change the Rails version specification in the Gemfile, and use bundle update rails.

Gemfile


# gem'rails','~> 5.0.7','>= 5.0.7.2'
gem'rails','~> 5.2.4','>= 5.2.4.2'

terminal


$ bundle update rails

3. Execute rails app:update task

Next run the rails app:update task. This task allows you to interactively create new files or modify existing files that are required by the new version.

terminal


$ rails app:update
    conflict config/boot.rb
Overwrite config/boot.rb? (enter "h" for help) [Ynaqdhm]

As mentioned above, it is a task to check what to do with files that cause conflicts one by one. Answer with any key of Y/n/a/q/d/h.

Y-Yes. Overwrite execution
n-No. Do not overwrite
a-All. Overwrite all files after this file
q-Quit. Processing interruption
d-Diff. Show diff of old and new files
h-Help. Display the meaning of each key to enter

“Troublesome to check each one…Is it all right?”

Yes, I thought so, and executed a = all. Then routes.rb was reborn (white day).

**Be sure to check only routes.rb! Rather, let’s set it to n! !! **

Mr. Ito also said,

I often check d = diff and then enter Y or n. (But maybe Y is input except for routes.rb)

4. Operation check (whether rails s starts)

Besides, it is necessary to check it with the test code and see it with your own eyes, but this time we aim at rails s (start the server in the development environment for the time being).

4-1. Error ①

I triumphantly performed rails s, but first from this error.

There is an error with require': cannot load such file --bootsnap/setup (LoadError).

terminal


$ rails s
Traceback (most recent call last):
3: from bin/rails:3:in `<main>'
2: from bin/rails:3:in `require_relative'
1: from /myapp/config/boot.rb:4:in `<top (required)>'
~~~config/boot.rb:4:in `require': cannot load such file --bootsnap/setup (LoadError)

I was able to cope with this method. There are two changes (file).

config/boot.rb


ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)

require'bundler/setup' # Set up gems listed in the Gemfile.
# Delete this line ↓ (or comment out)
require'bootsnap/setup' # Speed up boot time by caching expensive operations.

config/initializers/new_framework_defaults.rb


~
# Comment out this line ↓
# ActiveSupport.halt_callback_chains_on_return_false = false
~

4-2. Error ②

When I thought “Right now, the server is up!” and rails s again, I get an error again.

<top (required)>': undefined method `halt_callback_chains_on_return_false=' for ActiveSupport:Module (NoMethodError)

terminal


=> Booting Puma
=> Rails 5.2.4.3 application starting in development
=> Run `rails server -h` for more startup options
Exiting
Traceback (most recent call last):
~~~/config/initializers/new_framework_defaults.rb:23:in `<top (required)>': undefined method `halt_callback_chains_on_return_false=' for ActiveSupport:Module (NoMethodError)

I was able to respond here.

config/initializers/new_framework_defaults.rb


~
# This line ↓ Comment out
# Rails.application.config.action_controller.raise_on_unfiltered_parameters = true
~

Essential reading

The error I encountered this time is exactly what Mr. Ito is saying, so I think reading this article will help in the future.

Rails versions may introduce new behavior that is different from the old one. Ideally, after the version upgrade, all the behaviors will be adjusted to the new behavior, but in some cases it may be necessary to adapt some behaviors to the old Rails.

Change these behaviors in load_defaults and new_framework_defaults_x_x.rb.

The relationship between load_defaults and new_framework_defaults_x_x.rb is explained in detail in the following article, so please read here and change the settings appropriately.

Detailed investigation of the relationship between config.load_defaults and new_framework_defaults_x_x.rb-Qiita

Rails version upgrade completed

This completes Rails version upgrade. If the above steps do not work, try restarting the server (quit with ctrl + c, start with rails s).

5. What happens to the files around environment variables?

The file that defines environment variables was secrets.yml in Rails 5.0 series, but it is general to use credentials.yml.enc and master.key after Rails 5.2 series.

5-0. “That? No credentials or master key??”

I thought that if Rails version is upgraded, it will be made arbitrarily, but there was no such delicious story. In the first place, master.key is supposed to be created at the time of rails new, so it goes without saying…

5-1. Conclusion: You can keep secrets.yml

In particular, the environment variable file worked without any fiddling. Lol