DEV Community

Cover image for An In-Exhaustive List of Rails API Errors
Kailana Kahawaii
Kailana Kahawaii

Posted on • Edited on

An In-Exhaustive List of Rails API Errors

Introduction

Some time ago, I wrote up a guide for setting up a Rails API. If you are like me, then you usually encounter an error or two when following a tutorial. They happen: you may be running a different Ruby version, gem version, or database. I’ve compiled a list of errors I’ve run into in the past when creating an API for convenience.

I intend to update this list as I run into other errors.

List of Errors

Error: Ruby version errors.

Rails version errors affect compatibility with other programs, such as Heroku. You change the Ruby version by editing the number in your gem file.

ruby '2.6.6'

Error: gem version errors.

Sometimes you'll run into a gem version error. Your log will usually help you identify the culprit.

Solution: Update your gem versions in your gemfile. Delete your gemlock file then run

bundle install

Error: User unable to log in.

Bcrypt may be set up incorrectly.

  • See if the bcrypt gem is in the gemfile and that it is uncommented.

bcrypt

  • Ensure that the User table includes the password_digest attribute.

class CreateUsers < ActiveRecord::Migration[6.0]
  def change
    create_table :users do |t|
      t.string :username
      t.string :password_digest
      t.string :name
      t.timestamps
    end
  end
end

  • Check to see that the User model includes has_secure_password.

class User < ApplicationRecord
    has_secure_password
end

Error: Association errors.

Depending on the structure of schema, these errors can vary.

Here is a list of associations.

  • belongs_to
  • has_one
  • has_many
  • has_many :through
  • has_one :through
  • has_and_belongs_to_many

All has_many associations must be followed with an ‘s’. For example, if we create a User model that has many cats, the relationship would be expressed like this on the User model:

has_many :cats

The Cat model, which belongs to the User model, would be expressed this way:

belongs_to :user

It’s helpful to draw out a diagram before setting up your schema. Drawing out your models first may also reveal the need for has_many through relationships.

Error: Rails generator not working.

I ran into this error when setting up my latest api. I attempted to generate serializers and was met with an error message that looked something like this:

Running via Spring preloader in process 8893

I had never seen this error before. The spring gem was the culprit here. Restart the spring gem and try again.

Solution:
From your terminal, type the following:

bin/spring stop

bin/spring server

Routing Errors

Running rails generate resources followed by the model name will create RESTful routes for that resource. Convention states that API/V1 (or whichever version you're working on) precede the routes.

Solution:
Edit your routes.rb file so that resources are nested in :api and :v1 :

Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      resources :desks
      resources :courses
      resources :users
      post '/login', to: 'auth#create'
          get '/current_user', to: 'auth#show'
          post '/sign_up', to: 'users#create'
    end
  end
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

Database errors
Running rails db:create or rails db:migrate does not work.

The error:

PG::ConnectionBad: FATAL:  role "test_names" does not exist

Solution:

$ createuser -P -d -e test_names

Conclusion

This is a running list of the errors I encounter and how I go about fixing them. Got any Rails API errors to share? I'd love to hear about how you went about solving them.

Top comments (0)