我的应用程序有以下版本:rails 6.0.0
, ruby 2.6.3
.
我在应用程序中有大约 20 个控制器,我们计划在应用程序中使用fast-json-api
而不是rabl
作为序列化程序。我发现每个控制器的序列化程序文件数量增长得非常快。(下面的屏幕截图)。
问题
有没有更好的方法来组织所有的序列化器?
我尝试了以下两种组织方式:
在其模型名称下组织一个控制器的所有序列化程序(如 rabl)
但它给了我这个错误:
NameError (uninitialized constant Api::V1::LexiconsController::ListNodeSerializer)
我可以通过将所有 SerializerClassNames 保存在该控制器的同一个序列化器文件中来在单个序列化器中呈现不同的 api 视图吗?
类别控制器的串行器
class ShowSerializer < ApplicationSerializer
cache_options enabled: true, cache_length: 24.hours
attributes :name, :status, :description
end
class IndexSerializer < ApplicationSerializer
attributes :name, :status, :thumbnail, :definition
attributes :area do |area|
area.country.try(:name).try(:html_safe)
end
end