jsonapi-rb / jsonapi-rails

Rails gem for fast jsonapi-compliant APIs.
http://jsonapi-rb.org
MIT License
319 stars 63 forks source link

#<NoMethodError: undefined method `new' for nil:NilClass> during register_renderers? #72

Closed derekgstevens closed 6 years ago

derekgstevens commented 6 years ago

Ruby: 2.4.2 Rails: 5.1.4

I get the title error with the following stracktrace when trying to call render jsonapi: user

{
                "id": 0,
                "trace": "jsonapi-serializable (0.3.0) lib/jsonapi/serializable/renderer.rb:88:in `_build'"
            },
            {
                "id": 1,
                "trace": "jsonapi-serializable (0.3.0) lib/jsonapi/serializable/renderer.rb:82:in `build_resources'"
            },
            {
                "id": 2,
                "trace": "jsonapi-serializable (0.3.0) lib/jsonapi/serializable/renderer.rb:44:in `render'"
            },
            {
                "id": 3,
                "trace": "jsonapi-rails (0.3.1) lib/jsonapi/rails/renderer.rb:16:in `render'"
            },
            {
                "id": 4,
                "trace": "jsonapi-rails (0.3.1) lib/jsonapi/rails/railtie.rb:58:in `block (4 levels) in register_renderers'"
            },
            {
                "id": 5,
                "trace": "activesupport (5.1.4) lib/active_support/notifications.rb:168:in `instrument'"
            },
            {
                "id": 6,
                "trace": "jsonapi-rails (0.3.1) lib/jsonapi/rails/railtie.rb:55:in `block (3 levels) in register_renderers'"
            }

My UserController looks like this:

module Api::V1
  class UsersController < Api::V1::ApiController

    #POST /register
    def register
      user = User.create(user_params)
      if user.save
        render jsonapi: user
      else
        render jsonapi_errors: user.errors
      end
    end
  end
end

And my user_serializer.rb

class SerializableUser < JSONAPI::Serializable::Resource
  type 'users'

  attributes :email, :first_name, :last_name
end

I have also tried namespacing my SerializableUser with module Api::V1 but no luck. Any ideas what I'm doing wrong? I can see in the logs that the user is being created, and if I attempt to register the same user, the jsonapi_errors call works as expected:

{
    "errors": [
        {
            "title": "Invalid email",
            "detail": "Email has already been taken",
            "source": {}
        }
    ],
    "jsonapi": {
        "version": "1.0"
    }
}
beauby commented 6 years ago

@derekgstevens Have you found the source of the error? It looks like your SerializableUser is not being autoloaded because the constant name does not match the file name (user_serializer.rb).

dstevens-cs commented 6 years ago

@beauby yes, that was a silly mistake I made! Unfortunately, fixing that resulted in a different issue: "can't convert Symbol to Integer", also coming from serializable/renderer.rb:88. Haven't really had a chance to dig into it, however.

Thanks!