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

后端 未结 2 1081
独厮守ぢ
独厮守ぢ 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 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.

提交回复
热议问题