How to upgrade Rails 6.1 to Rails 7
Tags: rails, tutorial, webdev
Published: 2021-12-22 13:51:08

1. [Update rails in gemfile](#1-update-rails-in-gemfile)
2. [Upgrade rails packages](#2-upgrade-rails-packages)
3. [Add sprockets to gemfile](#3-add-sprockets-to-gemfile)
4. [Run update rails task](#4-run-update-rails-task)
5. [Verify framework defaults](#5-verify-framework-defaults)
6. [Verify depreciated methods](#6-verify-depreciated-methods)

---

How to upgrade an exising application to Rails 7. Note that Rails 7 requires Ruby 2.7.0 or newer.

---

## 1. Update rails in gemfile
```rb
# old
gem "rails", "~> 6.1.4"

# new
gem "rails", "~> 7.0.0"
```

In the terminal run:
```bash
bundle update
```

---

## 2. Upgrade rails packages
In the __package.json__ file, find all @rails packages and upgrade then one by one. For example:

```bash
yarn upgrade @rails/actioncable --latest
yarn upgrade @rails/activestorage --latest
yarn upgrade @rails/request.js --latest
```

---

## 3. Add sprockets to gemfile

When you are using the asset pipeline, assets related errors can pop up when running a rails command.
Example:
```bash
# - Don't know how to build task 'assets:precompile'
# - 'method_missing': undefined method `assets'
```

Add sprockets to the gem file as it's now an [optional depedency](https://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#sprockets-is-now-an-optional-dependency).

```rb
gem "sprockets-rails"
```

```bash
bundle install
```

---

## 4. Run update rails task
```bash
bin/rails app:update
```

This will prompt you file by file to integrate the new rails 7 defaults.

Example:
__Overwrite .../config/boot.rb? (enter "h" for help) [Ynaqdhm]__
Note: press `h` to see the menu options.

Go through every file to inspect the difference between the default set-up of rails 7 and your current configuration.

You can open the files in your editor to compare when selecting merge (m). If you run into the following error:
`Please specify merge tool to 'THOR_MERGE' env`
Run the app:update command again with your editor specified.

For example (vs code)
```bash
THOR_MERGE=code bin/rails app:update
```

After completing the set-up run `rails db:migrate` to migrate the newly added active storage migration file.

❗️ If you have set-up other environments, non-default enviroments, like __staging.rb__ make sure to update that file too.

---

## 5. Verify framework defaults

After completing the `bin/rails app:update` from the step 4, Rails creates a __new_framework_defaults_7.0.rb__ file in `config/initializers`. This file helps you to make a big upgrade a little easier by flipping on the new default settings one-by-one in multiple deployments.

The __application.rb__ file's setting __load_defaults__ specifies which rails version's default settings to load. This means you can temporarily utilize the default configuration settings from Rails 6.1 while running Rails 7. 

```rb
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.1
```

Then, gradually turn on each setting and verify your application is still working as intended. Once you've switched on every setting, you can remove the __new_framework_defaults_7.0.rb__ and flip the __load_defaults__ version in application.rb to 7.0.

[How to turn on Rails 7 framework defaults (part 1)](https://dev.to/thomasvanholder/rails-7-framework-defaults-part-1-2a59)

```rb
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 7.0
```

---

## 6. Verify depreciated methods

Your application might use other depreciated features. Read through the [Rails 6.1 to Rails 7 section](https://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#upgrading-from-rails-6-1-to-rails-7-0) to identify and fix any methods call or settings that are no longer supported.

---