无法使用 Grape Gem 自动加载序列化程序
Unable to autoload serializer using Grape Gem
我正在构建一个 API:
- Ruby 2.2
- Rails 4.2.6
- 葡萄-gem 0.16.2
- active_model_serializers-gem 0.10.2
- 葡萄-active_model_serializers-gem(1.4 来自大师)
我的 JSON 序列化程序运行良好,直到我尝试引入 API 版本控制,如下所述:https://github.com/ruby-grape/grape-active_model_serializers
未版本化的序列化程序:
class SignupSerializer < ActiveModel::Serializer
attributes :id, :comments, :pending
end
版本化序列化程序:
module Mobile
module V1
class SignupSerializer < ActiveModel::Serializer
attributes :id, :comments, :pending
end
end
end
错误信息是:
LoadError(无法自动加载常量 SignupSerializer,需要 /Users/username/GitHub/AppName/app/serializers/mobile/v1/signup_serializer.rb 来定义它):
app/api/mobile/logger.rb:16:in block in call'
app/api/mobile/logger.rb:15:in
call'
API 组织:
base.rb:
require 'grape-swagger'
module Mobile
class Dispatch < Grape::API
mount Mobile::V1::Root
mount Mobile::V2::Root
format :json
route :any, '*path' do
Rack::Response.new({message: 'Not found'}.to_json, 404).finish
end
add_swagger_documentation
end
Base = Rack::Builder.new do
use Mobile::Logger
run Mobile::Dispatch
end
end
v1/root.rb:
module Mobile
module V1
class Root < Dispatch
format :json
formatter :json, Grape::Formatter::ActiveModelSerializers
version 'v1'
default_error_formatter :json
content_type :json, 'application/json'
use ::WineBouncer::OAuth2
rescue_from :all do |e|
eclass = e.class.to_s
message = "OAuth error: #{e.to_s}" if eclass.match('WineBouncer::Errors')
status = case
when eclass.match('OAuthUnauthorizedError')
401
when eclass.match('OAuthForbiddenError')
403
when eclass.match('RecordNotFound'), e.message.match(/unable to find/i).present?
404
else
(e.respond_to? :status) && e.status || 500
end
opts = { error: "#{message || e.message}" }
opts[:trace] = e.backtrace[0,10] unless Rails.env.production?
Rack::Response.new(opts.to_json, status, {
'Content-Type' => "application/json",
'Access-Control-Allow-Origin' => '*',
'Access-Control-Request-Method' => '*',
}).finish
end
mount Mobile::V1::Me
mount Mobile::V1::Events
mount Mobile::V1::Signups
route :any, '*path' do
raise StandardError, 'Unable to find endpoint'
end
end
end
end
api/v1/signup.rb:
module Mobile
module V1
class Signups < Mobile::V1::Root
include Mobile::V1::Defaults
resource :signups, desc: 'Operations about the signups' do
desc "Returns list of signups"
oauth2 # This endpoint requires authentication
get '/' do
Signup.where(pending: false)
end
end
end
end
end
有什么想法吗?
gem 维护人员正在针对此问题进行更新。可以从存储库下载更新:
https://github.com/ruby-grape/grape-active_model_serializers/issues/55
我正在构建一个 API:
- Ruby 2.2
- Rails 4.2.6
- 葡萄-gem 0.16.2
- active_model_serializers-gem 0.10.2
- 葡萄-active_model_serializers-gem(1.4 来自大师)
我的 JSON 序列化程序运行良好,直到我尝试引入 API 版本控制,如下所述:https://github.com/ruby-grape/grape-active_model_serializers
未版本化的序列化程序:
class SignupSerializer < ActiveModel::Serializer
attributes :id, :comments, :pending
end
版本化序列化程序:
module Mobile
module V1
class SignupSerializer < ActiveModel::Serializer
attributes :id, :comments, :pending
end
end
end
错误信息是:
LoadError(无法自动加载常量 SignupSerializer,需要 /Users/username/GitHub/AppName/app/serializers/mobile/v1/signup_serializer.rb 来定义它):
app/api/mobile/logger.rb:16:in block in call'
app/api/mobile/logger.rb:15:in
call'
API 组织:
base.rb:
require 'grape-swagger'
module Mobile
class Dispatch < Grape::API
mount Mobile::V1::Root
mount Mobile::V2::Root
format :json
route :any, '*path' do
Rack::Response.new({message: 'Not found'}.to_json, 404).finish
end
add_swagger_documentation
end
Base = Rack::Builder.new do
use Mobile::Logger
run Mobile::Dispatch
end
end
v1/root.rb:
module Mobile
module V1
class Root < Dispatch
format :json
formatter :json, Grape::Formatter::ActiveModelSerializers
version 'v1'
default_error_formatter :json
content_type :json, 'application/json'
use ::WineBouncer::OAuth2
rescue_from :all do |e|
eclass = e.class.to_s
message = "OAuth error: #{e.to_s}" if eclass.match('WineBouncer::Errors')
status = case
when eclass.match('OAuthUnauthorizedError')
401
when eclass.match('OAuthForbiddenError')
403
when eclass.match('RecordNotFound'), e.message.match(/unable to find/i).present?
404
else
(e.respond_to? :status) && e.status || 500
end
opts = { error: "#{message || e.message}" }
opts[:trace] = e.backtrace[0,10] unless Rails.env.production?
Rack::Response.new(opts.to_json, status, {
'Content-Type' => "application/json",
'Access-Control-Allow-Origin' => '*',
'Access-Control-Request-Method' => '*',
}).finish
end
mount Mobile::V1::Me
mount Mobile::V1::Events
mount Mobile::V1::Signups
route :any, '*path' do
raise StandardError, 'Unable to find endpoint'
end
end
end
end
api/v1/signup.rb:
module Mobile
module V1
class Signups < Mobile::V1::Root
include Mobile::V1::Defaults
resource :signups, desc: 'Operations about the signups' do
desc "Returns list of signups"
oauth2 # This endpoint requires authentication
get '/' do
Signup.where(pending: false)
end
end
end
end
end
有什么想法吗?
gem 维护人员正在针对此问题进行更新。可以从存储库下载更新:
https://github.com/ruby-grape/grape-active_model_serializers/issues/55