It was recently recommended to me by a mentor to build a Rails backend API as another practice project to continue my web development learning and skill building. Once the API is built (correctly I may add), other frontend standalone applications, whether they’re based on simple static html/css pages or more dynamic javascript frameworks like EmberJS or ReactJS, should be able to interface with it to store and retrieve data seamlessly as if they were fully integrated applications. I wanted to create something relatively quick and simple so that I could focus on the high level moving parts of a Rails API and not so much on the “next best app”, so I decided to build a todo list.

While doing a few Google searches to learn how a Rails API differs from a standard Rails app, I learned that the Rails API was added to the recently released Rails 5.0.0.rc standard library. For earlier versions, Rails API was available as a gem. I thought this was a good reason to download the latest Rails version and give it a try. While I ultimately downloaded the new version and got my Rails API up in running with relative ease, I did have a few hiccups and thought that I’d share what they were and how I fixed it.

As an entry-level Ruby on Rails developer, one thing I have not dealt with a lot is managing different Ruby and Rails versions across multiple applications as well as all the different dependencies that come with them. This is where I struggled a little after downloading the latest version of Rails. For all of my previous projects, I had upgraded Ruby and Rails only once. Other than having to run the command bundle exec before running some commands like rspec, I didn’t have much trouble with the different versions and dependencies. For the last year or so, I knew trouble would come eventually because I kept coming across the term “dependency management” on Stack Overflow and other websites.

Since I wanted to use Rails 5 to create the new Rails API todo list app and not the rails-api gem with the older version of Rails that I currently had installed, I needed to download the new version first before running the rails new command. I used the following command (along with specifying which Ruby version to use) to try to download the new Rails version:

$ RBENV_VERSION=2.3.0 rbenv exec gem install rails --version 5.0.0.rc1

This resulted in the following output:

ERROR:  While executing gem ... (Gem::DependencyResolutionError)
    conflicting dependencies activesupport (= 3.0.0) and activesupport (= 5.0.0.rc1)
  Activated activesupport-5.0.0.rc1
  which does not match conflicting dependency (= 3.0.0)

  Conflicting dependency chains:
    rails (= 5.0.0.rc1), 5.0.0.rc1 activated, depends on
    activesupport (= 5.0.0.rc1), 5.0.0.rc1 activated

  versus:
    rails (= 5.0.0.rc1), 5.0.0.rc1 activated, depends on
    sprockets-rails (>= 2.0.0), 2.0.0 activated, depends on
    actionpack (>= 3.0), 3.0.0 activated, depends on
    activesupport (= 3.0.0)

While this may be a trivial situation for a seasoned Rails developer, I didn’t immediately know how to resolve the dependency resolution error. Nothing that a few Google searches can’t handle I thought. Well, it turned out that the fixes that my searches turned up didn’t fix my particular situation. This didn’t bother me too much because this is programming, and there aren’t always answers to every bug question I have. This is where I know I need to extract information from a combination of multiple answers or fixes, and read any documentation that I may need to freshen up on or learn about for the first time in order to come up with a solution.

I first tried updating the RubyGems system software to see if this would fix the issue:

$ gem update --system

I tried installing the new Rails version again and had the same error as above. I tried updating the specific gems that were causing the conflicting dependencies error:

$ gem update actionpack
Updating installed gems
Nothing to update
$ gem update activesupport
Updating installed gems
Nothing to update
$ gem update sprockets-rails
Updating installed gems
Nothing to update

I of course still received the same error after trying the install again. Some answers on Stack Overflow for similar dependency issues recommended uninstalling specific gems so I tried that next:

$ gem uninstall sprockets-rails
$ gem uninstall actionpack

I tried downloading the new Rails version again:

$ RBENV_VERSION=2.3.0 rbenv exec gem install rails --version 5.0.0.rc1

This time it worked! I then ran bundle install on a previous app that had Rails 4.2.0 declared in the Gemfile and the two older gems I uninstalled were reinstalled. I ran gem list and there were indeed the old version and version 5.0.0.rc1 of both the actionpack and sprockets-rails gems.

From there, I was able to run rails _5.0.0.rc1_ new todo-list-api --api -T to create my new Rails API backend application. Again, I know more experienced Rails developers may find this as a simple bug to fix and most likely know better ways to manage dependencies than I have described here. I thought I’d share how I fixed it in case some other beginner out there has the same issues with the recently released Rails 5 and wants a quick fix.