Jsonapi-resources: `load_missing_constant': Unable to autoload constant

Created on 8 Jan 2016  路  3Comments  路  Source: cerebris/jsonapi-resources

When I add a jsonapi route

Rails.application.routes.draw do
  namespace :api, path: '/', constraints: { subdomain: 'api' } do  
    jsonapi_resources :projects
    # ...
  end
end

Rails produces a following error during the app initializtion:

..ruby-2.1.4@rails4/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:481:in `load_missing_constant': Unable to autoload constant ProjectResource, expected .../app/resources/project_resource.rb to define it (LoadError)

My ProjectController (resides in app/controller/api directory):

module API
    class ProjectsController < ApplicationController
      before_action :set_project, only: [:show, :update, :destroy]
      respond_to :json
      # GET /projects
      # GET /projects.json
      def index
        respond_with Project.all
      end

       # ...
end

And a resource defenition (app/resources/project_resource.rb):

class API::ProjectResource < JSONAPI::Resource
  attributes :name, :created_at, :updated_at
end

What did I miss?

Most helpful comment

So glad for your comment @matthias-g!

Here's how I got there for anyone following in these footsteps.

Put my definitions here:
app/resources/api/v1/project_resource.rb

And the controller here:
app/controllers/api/v1/project_controller.rb

class Api::V1::ProjectResource < JSONAPI::Resource

end

All 3 comments

You need to derive the controller from JSONAPI::ResourceController or include include JSONAPI::ActsAsResourceController in your controller.

I just came across the same problem. For future reference: To solve the problem I had to put the resource definition into app/resources/api/project_resource.rb

So glad for your comment @matthias-g!

Here's how I got there for anyone following in these footsteps.

Put my definitions here:
app/resources/api/v1/project_resource.rb

And the controller here:
app/controllers/api/v1/project_controller.rb

class Api::V1::ProjectResource < JSONAPI::Resource

end
Was this page helpful?
0 / 5 - 0 ratings