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:
This resulted in the following output:
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:
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:
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:
I tried downloading the new Rails version again:
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.