【问题标题】:Ember serializers not working with Rails 4.2Ember 序列化程序不适用于 Rails 4.2
【发布时间】:2015-01-09 02:41:48
【问题描述】:

更新到 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 配置不再起作用了吗?

【问题讨论】:

    标签: ruby-on-rails ember-data active-model-serializers ruby-on-rails-4.2


    【解决方案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 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

    【讨论】:

      猜你喜欢
      • 2015-11-22
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-17
      • 2012-10-26
      • 2020-04-20
      相关资源
      最近更新 更多