ruby-on-railsjson-apijsonapi-resources

How i can fix JSONAPI::Serializable::UndefinedSerializableClass


How I can fix:

ERROR: JSONAPI::Serializable::UndefinedSerializableClass: No 
serializable class defined for Post
app/controllers/api/v1/posts_controller.rb:20:in `index'

Using this gems:

gem 'jsonapi_suite'
gem 'jsonapi-rails'

Solution

  • You need to set a class argument to render_jsonapi.

    # app/controllers/api/v1/posts_controller.rb
    module Api
      module V1
        class PostsController < Api::V1::ApplicationController
          # ...
    
          def index
            posts = Post.all
            render_jsonapi(posts, class: { Post: Api::V1::SerializablePost })
          end
    
          # ...
        end
      end
    end