JsonApi Serializer Not Found

478 views Asked by At

I have run gem 'jsonapi-serializer' and bundle install.

I then try to run a generator rails g serializer User username id but the command always freezes. So I created the serializer manually:

# app/serializers/user_serializer.rb
class UserSerializer
  include JSONAPI::Serializer
  attributes :username, :id
end

but when I call it from my UsersController I get an error:

# app/controllers/user_controller.rb
class UsersController < ApplicationController
  def show
    render json: { user: UserSerializer.new(User.new(username: 'joe'))}
  end
end

here the error:

NameError in UsersController#show
uninitialized constant UsersController::UserSerializer

It seems that the UserSerializer is not loading, and I am not sure where I can tinker to make that work.

1

There are 1 answers

0
user3755529 On

I think it was a problem with the spring loader.

I found this solution to work for me: https://stackoverflow.com/a/60688929/3755529