[英]"uninitialized constant Api::V1::ReservationOptionsController::ReservationOptions"
[英]uninitialized constant API::V1::ApplicationSerializer
我開始使用gem 'active_model_serializers', '~> 0.10.0'
。 按照本文檔執行。
我有以下關系。
landmark has_many concerns
concern belongs_to landmark
concern has_many comments
我在app/serializers/api/application_serializer.rb
創建了一個ApplicationSerializer
module API
class ApplicationSerializer < ActiveModel::Serializer
# some commode here
end
end
我的關注序列化程序包含:
# ===> Does not work <=====
module API::V1
class ConcernSerializer < ApplicationSerializer
attributes :id, :body
has_many :comments
belongs_to :landmark
class CommentSerializer < ApplicationSerializer
attributes :id, :body
end
class LandmarkSerializer < ApplicationSerializer
attributes :id, :short_address
end
end
end
# ====> However, this works <====
module API::V1
class ConcernSerializer < ApplicationSerializer
attributes :id, :body
has_many :comments
class CommentSerializer < ActiveModel::Serializer
attributes :id, :body
end
end
end
# ===> Does not work again<=====
module API::V1
class ConcernSerializer < ApplicationSerializer
attributes :id, :body
has_many :comments
belongs_to :landmark
class CommentSerializer < ActiveModel::Serializer
attributes :id, :body
end
class LandmarkSerializer < ActiveModel::Serializer
attributes :id, :short_address
end
end
end
在上面的代碼中,如果我用ActiveModel::Serializer
替換ApplicationSerializer
它可以正常工作,但是我丟失了ApplicationSerializer
中定義的通用代碼。
看起來像一個自動加載問題。 ApplicationSerializer
應該在它自己的文件<serializers directory>/api/application_serializer.rb
(確保在app
中創建目錄后重新啟動 server/spring 以便它們被自動加載器拾取)
您也可以使用絕對 class 名稱來引用它: ::API::ApplicationSerializer
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.