You are here

How to Update the Rails-API to Rails 5 in API Mode

The rails-api library has been abandoned since it was merged into Rails 5. That means developers with rails-api apps need to move them to Rails 5. Aaron Gray over at his blog shows you how.

Getting Started

First off, you’re going to want to create a new Git branch to commit your update changes while you’re working. If you haven’t already, you should also now upgrade Ruby to 2.2.2. 

Secondly, and this is a little tricky, you’re going to have to update your app to the latest version of rails-api and Rails (0.4.0 and at the time of writing). To update Rails, you’re going to want to move from one incremental version to the next, running tests as you go and fixing deprecation warnings as they come up before you move to the next version. At each step, change the rails version in the GemFile and run ‘bundle update’ to check everything’s okay. 

Then change the version of Rails in your GemFile to ‘gem 'rails', '5.0.0'’ and run ‘bundle update rails’. If that fails, you’ll probably need to update app dependencies. If a gem doesn’t support Rails 5 in any update, you’ll have to remove the gem and find some way to get the same functionality without it. 

Removing Rails-API

You’ll then need to remove the rails-api gem from the GemFile and any old versions of dependencies that stuck around while you were updating. You now effectively have a Rails 5 app. But you wanted a Rails 5 api app. The next step is switch from the one to the other.

Moving From a Rails 5 App to a Rails 5 API App

To do this, in the application class of the config/application.rb file, add ‘config.api_only = true’. At the same time, go to the development class in the config/environments/development.rb file and add ‘config.debug_exception_response_format = :api’. Then create a new file application_record.rb in the models folder and add the following:

Be sure to read the next Application Development article: How to Hide API Keys When Building Web Apps On CodePen

Original Article