简体   繁体   中英

Ember serializers not working with Rails 4.2

Updated to Rails 4.2 and now I cannot for the life of me get the ActiveModel::Serializer configuration to work correctly.

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

Previously, this worked great with:

respond_with @thing

With 4.2 (and 0.9.2 AMS), you have to say:

respond_with @thing, root: true

explicitly. Anyone understand why the global embed_in_root config no longer works?

I had the same trouble...

It appears that active model serializers 0.9.2 is not compatible with Rails 4.2.

What I think may be happening in your case is when you call:

respond_with @thing, root: true 

you are not using the Active Model Serializers gem at all. I tested this by adding a custom attribute in my active model serializer, like this:

class ThingSerializer < ActiveModel::Serializer
  attributes :this_is_a_test

  def this_is_a_test
      "and it does not work"  
  end                       

end

The this_is_a_test attribute was not turning up in my JSON so I realized that the active model serializer was not in use.

I followed igagnidz and added this to my application controller:

def _render_with_renderer_json(json, options)
  serializer = build_json_serializer(json, options)

  if serializer
    super(serializer, options)
  else
    super(json, options)
  end

end

This is what finally got me through it: https://github.com/rails-api/active_model_serializers/issues/641

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM