我有同样的麻烦...
看来活动模型序列化程序 0.9.2 与 Rails 4.2 不兼容。
我认为您的情况可能会发生在您致电时:
respond_with @thing, root: true
您根本没有使用 Active Model Serializers gem。我通过在我的活动模型序列化程序中添加一个自定义属性来测试这一点,如下所示:
class ThingSerializer < ActiveModel::Serializer
attributes :this_is_a_test
def this_is_a_test
"and it does not work"
end
end
我的this_is_a_test attribute
JSON 中没有出现,所以我意识到活动模型序列化程序没有被使用。
我跟随 igagnidz 并将其添加到我的应用程序控制器中:
def _render_with_renderer_json(json, options)
serializer = build_json_serializer(json, options)
if serializer
super(serializer, options)
else
super(json, options)
end
end
这就是最终让我度过难关的原因:https ://github.com/rails-api/active_model_serializers/issues/641