添加 jsonapi-suite 后获取“未初始化常量 JsonapiCompliable::Adapters::ActiveRecord)”
Getting `uninitialized constant JsonapiCompliable::Adapters::ActiveRecord)` after adding jsonapi-suite
如何使 JsonapiCompliable::Adapters::ActiveRecord 可用?
$ curl localhost:3001/组
产生这个:
ActionController::RoutingError (uninitialized constant JsonapiCompliable::Adapters::ActiveRecord):
app/resources/application_resource.rb:9:in `<class:ApplicationResource>'
app/resources/application_resource.rb:5:in `<main>'
app/resources/group_resource.rb:1:in `<main>'
app/controllers/groups_controller.rb:3:in `<class:GroupsController>'
app/controllers/groups_controller.rb:1:in `<main>'
我已将此代码添加到我现有的 Rails 5.2.0 项目中:
Gemfile
gem 'jsonapi_suite', '~> 0.7'
gem 'jsonapi-rails', '~> 0.3.0'
gem 'jsonapi_swagger_helpers', '~> 0.6', require: false
gem 'jsonapi_spec_helpers', '~> 0.4', require: false
gem 'kaminari', '~> 1.0'
ApplicationController
class ApplicationController < ActionController::API
+ # Bootstrap jsonapi_suite with relevant modules
+ include JsonapiSuite::ControllerMixin
+
+ register_exception JsonapiCompliable::Errors::RecordNotFound,
+ status: 404
+
+ # Catch all exceptions and render a JSONAPI-compliable error payload
+ # For additional documentation, see https://jsonapi-suite.github.io/jsonapi_errorable
+ rescue_from Exception do |e|
+ handle_exception(e)
+ end
+
GroupsController
class GroupsController < ApplicationController
jsonapi resource: GroupResource
strong_resource :group
before_action :apply_strong_params, only: [:create, :update]
def index
groups = Group.all
render_jsonapi(groups)
end
def show
scope = jsonapi_scope(Group.where(id: params[:id]))
instance = scope.resolve.first
raise JsonapiCompliable::Errors::RecordNotFound unless instance
render_jsonapi(instance, scope: false)
end
def create
group, success = jsonapi_create.to_a
if success
render_jsonapi(group, scope: false)
else
render_errors_for(group)
end
end
def update
group, success = jsonapi_update.to_a
if success
render_jsonapi(group, scope: false)
else
render_errors_for(group)
end
end
def destroy
group, success = jsonapi_destroy.to_a
if success
render json: { meta: {} }
else
render_errors_for(group)
end
end
end
app/resources/group_resource.rb
class GroupResource < ApplicationResource
type :groups
end
从控制台调试
[4] pry(main)> JsonapiCompliable::Adapters
=> JsonapiCompliable::Adapters
[5] pry(main)> JsonapiCompliable::Adapters::ActiveRecord
NameError: uninitialized constant JsonapiCompliable::Adapters::ActiveRecord
那么,为什么 JsonapiCompliable::Adapters::ActiveRecord 不可用?
app/resources/application_resource.rb
class ApplicationResource < JsonapiCompliable::Resource
use_adapter JsonapiCompliable::Adapters::ActiveRecord
end
添加
require 'jsonapi_compliable/adapters/active_record'
到config/initializers/jsonapi.rb
。如果您 运行 快速入门中的生成器,您会看到这一点。
如何使 JsonapiCompliable::Adapters::ActiveRecord 可用?
$ curl localhost:3001/组
产生这个:
ActionController::RoutingError (uninitialized constant JsonapiCompliable::Adapters::ActiveRecord):
app/resources/application_resource.rb:9:in `<class:ApplicationResource>'
app/resources/application_resource.rb:5:in `<main>'
app/resources/group_resource.rb:1:in `<main>'
app/controllers/groups_controller.rb:3:in `<class:GroupsController>'
app/controllers/groups_controller.rb:1:in `<main>'
我已将此代码添加到我现有的 Rails 5.2.0 项目中:
Gemfile
gem 'jsonapi_suite', '~> 0.7'
gem 'jsonapi-rails', '~> 0.3.0'
gem 'jsonapi_swagger_helpers', '~> 0.6', require: false
gem 'jsonapi_spec_helpers', '~> 0.4', require: false
gem 'kaminari', '~> 1.0'
ApplicationController
class ApplicationController < ActionController::API
+ # Bootstrap jsonapi_suite with relevant modules
+ include JsonapiSuite::ControllerMixin
+
+ register_exception JsonapiCompliable::Errors::RecordNotFound,
+ status: 404
+
+ # Catch all exceptions and render a JSONAPI-compliable error payload
+ # For additional documentation, see https://jsonapi-suite.github.io/jsonapi_errorable
+ rescue_from Exception do |e|
+ handle_exception(e)
+ end
+
GroupsController
class GroupsController < ApplicationController
jsonapi resource: GroupResource
strong_resource :group
before_action :apply_strong_params, only: [:create, :update]
def index
groups = Group.all
render_jsonapi(groups)
end
def show
scope = jsonapi_scope(Group.where(id: params[:id]))
instance = scope.resolve.first
raise JsonapiCompliable::Errors::RecordNotFound unless instance
render_jsonapi(instance, scope: false)
end
def create
group, success = jsonapi_create.to_a
if success
render_jsonapi(group, scope: false)
else
render_errors_for(group)
end
end
def update
group, success = jsonapi_update.to_a
if success
render_jsonapi(group, scope: false)
else
render_errors_for(group)
end
end
def destroy
group, success = jsonapi_destroy.to_a
if success
render json: { meta: {} }
else
render_errors_for(group)
end
end
end
app/resources/group_resource.rb
class GroupResource < ApplicationResource
type :groups
end
从控制台调试
[4] pry(main)> JsonapiCompliable::Adapters
=> JsonapiCompliable::Adapters
[5] pry(main)> JsonapiCompliable::Adapters::ActiveRecord
NameError: uninitialized constant JsonapiCompliable::Adapters::ActiveRecord
那么,为什么 JsonapiCompliable::Adapters::ActiveRecord 不可用?
app/resources/application_resource.rb
class ApplicationResource < JsonapiCompliable::Resource
use_adapter JsonapiCompliable::Adapters::ActiveRecord
end
添加
require 'jsonapi_compliable/adapters/active_record'
到config/initializers/jsonapi.rb
。如果您 运行 快速入门中的生成器,您会看到这一点。