1

更新到 Rails 4.2,现在我无法让 ActiveModel::Serializer 配置正常工作。

ActiveModel::Serializer.setup do |config|
  config.embed = :ids
  config.embed_in_root = true
end

以前,这适用于:

respond_with @thing

对于 4.2(和 0.9.2 AMS),您必须说:

respond_with @thing, root: true

明确地。任何人都明白为什么全局embed_in_root配置不再起作用?

4

1 回答 1

1

我有同样的麻烦...

看来活动模型序列化程序 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 attributeJSON 中没有出现,所以我意识到活动模型序列化程序没有被使用。

我跟随 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

于 2015-01-11T03:31:49.470 回答