Using ActiveModel::Serializer in Rails - JSON data differs between json and index response

后端 未结 2 1058
独厮守ぢ
独厮守ぢ 2021-01-01 05:21

I\'m using active_model_serializers gem to control the serialization data, and seeing some odd behavior. My code looks like so:

model & serializer



        
相关标签:
2条回答
  • 2021-01-01 05:50

    @Gagan this works for me:

    App.users = new App.Collections.Users(<%= ActiveModel::ArraySerializer.new(@users).to_json.html_safe %>);

    0 讨论(0)
  • 2021-01-01 06:05

    You are not using your serializer in the HTML view. Try this:

    App.users = new App.Collections.Users(<%= UserSerializer.new(@users).to_json.html_safe %>);

    The reason for this is that the serializer is picked up in the respond_with method, the serializer does not overwrite your .to_json method.

    0 讨论(0)
提交回复
热议问题