jsonapi-rails icon indicating copy to clipboard operation
jsonapi-rails copied to clipboard

uninitialized constant GroupsController::SerializableGroup

Open chrisgeek opened this issue 5 years ago • 4 comments
trafficstars

Hi, I am trying to switch from AMS to jsonapi-rails gem, but after creating serializers, I still get an uninitialized constant GroupsController::SerializableGroup error message from the gem

def index
    groups = Group.all
    render jsonapi: groups, class: {Group: SerializableGroup } 
end

serializers/group_serializer.rb

class SerializableGroup < JSONAPI::Serializable::Resource
  type 'group'
  has_many :users
  attributes :name
end

I'm not sure what I am doing wrong really.

chrisgeek avatar Feb 05 '20 13:02 chrisgeek

Could you try replacing class: {Group: SerializableGroup } with class: {Group: ::SerializableGroup } ?

beauby avatar Feb 05 '20 14:02 beauby

Thanks for your response, tried what you suggested but I got the same error message.

chrisgeek avatar Feb 05 '20 14:02 chrisgeek

Maybe a Rails autoloading thing? Is there a require in the controller for the serializers/group_serializer file?

Based on the original issue it looks like the name of the file (serializers/group_serializer.rb) does not match the name of the class (SerializableGroup) both because of the ordering of terms group and serializable and because the file name is serializers while the class says Serializable so the Rails autoloader might not be able to find and load that class dynamically.

matt-glover avatar Mar 30 '20 20:03 matt-glover

I had the same issue. Changed the name of the file to match the new Serializable class (Like this: serializable_equipment for the class: SerializableEquipment) and it worked for me.

It is as @matt-glover implied. A rails autoloading thing with the filename

JohnFTitor avatar Jun 04 '22 01:06 JohnFTitor