2

I'm trying to add a namespace to my 'Category' controller and resource.

So the first thing I did was to move the categories_controller.rb to app/controllers/api/v1/categories_controller and the category_resource.rb to app/resources/api/v1/

And then I redeclared these artifacts as following:

Controller

module Api
  module V1
    class CategoriesController < ApplicationController
      #before_action :doorkeeper_authorize!
    end
  end
end

Resource

module Api
  module V1
    class CategoryResource < JSONAPI::Resource
      attribute :name
    end
  end
end

And in routes.rb I moved the categories route to

namespace :api do
  namespace :v1 do
    jsonapi_resources :categories
  end
end

I already got different erros trying to solve this issue. To the current configuration, this is the error I get:

JSONAPI: Could not find resource 'categories'. (Class CategoryResource not found) (NameError)

What am I doing wrong?

Victor Ferreira
  • 6,151
  • 13
  • 64
  • 120

2 Answers2

0

Based on the documentation here (https://github.com/cerebris/jsonapi-resources) you should not move the resource.

And it should not be in the modules.

Albin
  • 2,912
  • 1
  • 21
  • 31
0

Your code looks fine - I have something similar with jsonapi-resources 0.7.0:

class Api::V1::UsersController
...
class Api::V1::UserResource < BaseResource
...
namespace :api do
namespace :v1 do
  jsonapi_resources :users do
    jsonapi_relationships
  end

is it possible your rails load path is trying to load the api/v1 directory directly rather than treating it as a module subfolder?

Ilya Kaplun
  • 51
  • 1
  • 5