[RUBY] [Rails] I tried to raise the Rails version from 5.0 to 5.2

Thing you want to do

I want to upgrade from Rails 5.0 series to Rails 5.2 series. (I wanted to use Active Storage that has been available since Rails 5.2.)

The purpose of this article is to introduce ** the steps required to upgrade from Rails 5.0 series to Rails 5.2 series ** and ** error handling ** that occurred at that time (and I want to use 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! ?? Mr. Ito's Rails App Upgrade Procedure

Basically, it is easy to understand if you look at Mr. Ito's article (falling over). However, I think that there are quite a few people who say, "I want to upgrade the version as soon as possible !!", so I hope you can see it in an evil way.

Also, it should cover up to the test, but this time ** the purpose is to use Active Storage **, so I will omit that.

1. Upgrade gems other than Rails

If you upgrade the version of Rails itself first, peripheral gems such as Devise and Carrierwave are not compatible with the latest Rails, and unexpected errors may occur.

Yes. It just happened. Lol

1-1. Update the development and test group gems first

terminal


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

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

The bundle outdated command will list the gems that are out of date. However, if the number of gems is not so large, you may update it all at once.

If you want 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

bundle update without specifying a gem name.

Gemfile


#Leave the Rails version specification unchanged
gem 'rails', '~> 5.0.7', '>= 5.0.7.2'

After confirming that the Rails version is specified, bundle update

terminal


#Update gems in bulk
$ bundle update

2. Raise the Rails version

Change the Rails version specification in the Gemfile to 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. Run the rails app: update task

Then run the rails app: update task. Perform this task to interactively create new files or modify existing files as 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 one by one how to handle files that cause conflicts. Answer with one of the keys 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 diffs of old and new files
h -Help. Show the meaning of each key you enter

"It's a hassle to check this one by one ... can I do it all together?"

Yes, I thought so too and ran ʻa= all. Thenroutes.rb` was reborn (Hakujitsu).

** Make sure to check only routes.rb! Rather, let's make it n! !! ** **

Mr. Ito also said this.

I often check d = diff and then type Y or n. (But most of the Y may be entered except for routes.rb)

4. Operation check (whether rails s starts)

Other than that, you need to check the test code and see it with your own eyes, but this time I will aim for rails s (start the server in the development environment for the time being).

4-1. Error ①

When I triumphantly rails s, first of all, from this error.

require': cannot load such file --error with 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 respond by this method. There are two changes (files).

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 "Now, it's time to start the server!" And rails s, I got an error again.

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




#### **`terminal`**
```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
~

Must-read article

The error I encountered this time is exactly what Mr. Ito said, so I think it will be useful in the future if you read and understand this article.

As the version of Rails goes up, new behavior that is different from the conventional behavior may be introduced. Ideally, after the version upgrade, everything will be adapted to the new behavior, but in some cases it may be necessary to adapt some behavior to the old Rails.

These behavior changes are made 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 this and change the settings appropriately.

I examined the relationship between config.load_defaults and new_framework_defaults_x_x.rb in detail --Qiita

Rails version upgrade is complete

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

5. What happens to the files around the environment variables?

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

5-0. "Huh? No credentials or master key ??"

I thought that it would be possible to make it without permission by upgrading the version of Rails, but there was no such delicious story. In the first place, master.key should be created at the time of rails new, so it is natural to say that ...

5-1. Conclusion: You can leave it as secrets.yml

Especially the environment variable file worked without tampering. Lol

Recommended Posts

[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
I tried to explain the method
[Rails] I tried deleting the application
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
[JDBC] I tried to access the SQLite3 database from Java.
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
I tried upgrading from CentOS 6.5 to CentOS 7 with the upgrade tool
I tried to summarize the methods used
I tried to introduce CircleCI 2.0 to Rails app
I tried to implement the Iterator pattern
I tried to summarize the Stream API
I tried to build Ruby 3.0.0 from source
I tried to sort the data in descending order, ascending order / Rails
I tried to implement the image preview function with Rails / jQuery
I tried to understand how the rails method "redirect_to" is defined
I tried to understand how the rails method "link_to" is defined
I tried to solve the paiza campaign problem "Challenge from Kaito 813"
The code I used to connect Rails 3 to PostgreSQL 10
I want to play with Firestore from Rails
I tried to set tomcat to run the Servlet.
I tried to get the distance from the address string to the nearest station with ruby
[JDBC ③] I tried to input from the main method using placeholders and arguments.
I tried to organize the cases used in programming
[Java] I want to calculate the difference from the date
Tokoro I rewrote in the migration from Wicket 7 to 8
I tried to summarize the state transition of docker
I tried to decorate the simple calendar a little
[Rails] I tried playing with the comment send button
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
[Rails] I don't know how to use the model ...
Rails6 I tried to introduce Docker to an existing application
I tried to implement the Euclidean algorithm in Java
I tried Rails beginner [Chapter 1]
I tried the Docker tutorial!
I tried the VueJS tutorial!
I tried the FizzBuzz problem
I tried Rails beginner [Chapter 2]
I tried to verify yum-cron
I tried to make an application in 3 months from inexperienced
I tried to implement the like function by asynchronous communication
[Rails] I tried using the button_to method for the first time
I tried to increase the processing speed with spiritual engineering
[Rails] [bootstrap] I want to change the font size responsively
I tried to summarize the basics of kotlin and java
[Rails] I tried to create a mini app with FullCalendar
[Swift] I tried to implement the function of the vending machine
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I want to create a form to select the [Rails] category
I tried to summarize the basic grammar of Ruby briefly
I tried to build the environment little by little using docker
[Rails] I tried to implement batch processing with Rake task
I tried to build the environment of WSL2 + Docker + VSCode
[Rails] I tried to implement "Like function" using rails and js
I tried validation to unify the way hashtags are written
[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
What I learned from studying Rails
I tried to chew C # (indexer)
I tried to summarize iOS 14 support
I tried to interact with Java