【发布时间】:2016-10-28 18:48:20
【问题描述】:
我正在将 gem active_model_serializers 从版本 0.9.5 更新到 0.10.1。对于 0.9.5 版,以下代码有效。
控制器:
def create
...
render json: @dia, app_rights: app_rights(@dia)
end
序列化器:
class Api::V1::SerializerWithSessionMetadata < ActiveModel::Serializer
attributes :app_rights
def app_rights
serialization_options[:app_rights]
end
end
方法serialization_options 已在版本 0.10.1 中弃用。
-
Here 建议改用
instance_options。 -
Here 建议使用
options:“instance_options 仅在 master 分支中可用,在当前 RC 中不可用。在当前 RC 中,您必须使用 options 代替”。 - 还有针对
@options和@instance_options的建议。
我已尝试将serialization_options 替换为上述所有选项。但是,在所有情况下,更新 gem 后,生成的 json 都不包含app_rights。我做错了什么?
【问题讨论】:
-
你使用的是什么 Rails 和 ruby 版本?根据更改日志对 Rails 4.0 和 ruby 2.0.0 的支持已被删除
-
使用最新版本:ruby 2.3.1 和 rails 4.2.6
-
hmmm 有趣的是,他们说 0.10.x 不向后兼容 0.9 或 0.8,可能会提交“缺少迁移指南”的错误?
-
答案是here
-
简短回答:改用
instance_options
标签: ruby-on-rails ruby json active-model-serializers