I have a Rails API and after starting the server on the first request I am receiving the following error:
Unable to autoload constant Api::V1::UsersController, expected /Users/user/Sites/project/api/app/controllers/api/v1/users_controller.rb to define it
If I then continue to make requests everything loads as normal without errors.
I am using Ruby 2.7.1 and Rails 5.1.7 for just an API application.
I have the following routes defined:
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
# User Routes
post 'create_user' => 'users#create'
get 'get_user' => 'users#show'
delete 'destroy_user' => 'users#destroy'
put 'update_user' => 'users#update'
# Login Route
post 'login' => 'user_token#create'
end
end
end
My folder structure is
project
- app
- controllers
- api_controller.rb
- application_controller.rb
- Api
- V1
- users_controller.rb
- models
- user.rb
The User's controller it is setup like so (I have also tried inline Api::V1::UsersController inheritance):
module Api
module V1
class UsersController < ApiController
before_action :authenticate_user, only: [:show, :update, :destroy]
def create
@user = User.new(user_params)
if @user.save
render json: { status: 200, data: @user }, status: :ok
else
render json: { status: 422 }, status: :unprocessable_entity
end
end
def show
render json: { status: 200, data: current_user }, status: :ok
end
def update
if current_user.update(user_params)
render json: { status: 200, data: current_user }, status: :ok
else
render json: { status: 422 }, status: :unprocessable_entity
end
end
def destroy
if current_user.delete
render json: { status: 200, data: {} }, status: :ok
else
render json: { status: 422 }, status: :unprocessable_entity
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :first_name, :last_name)
end
end
end
end
Api controller looks like this:
class ApiController < ActionController::API
include Knock::Authenticable
end
Application controller like this:
class ApplicationController < ActionController::API
end
User model looks like this:
class User < ApplicationRecord
has_secure_password
end
Any help would be much appreciated as this structure looks correct to me