Error sideloading JSON for act-as-taggable tags using active-model-serializers

后端 未结 1 1677
耶瑟儿~
耶瑟儿~ 2021-02-09 04:42

In a Ruby 2/Rails 4 app, I am trying to use acts-as-taggable-on in conjunction with active_model_serializers in order to create a JSON API that would output my tags along with o

相关标签:
1条回答
  • 2021-02-09 05:07

    Fixed! Turns out to sideload the tags, the corresponding serializer must be defined. I did not know this, because the documentation seems to imply that having a serializer is optional, and some default will be used in the absence of one. Apparently this is not the case if you wish to use the include: true option. Key in sight came from here, thanks very much!

    For completeness, I'll show what I did. I created tag_serializer.rb with the following code:

    module ActsAsTaggableOn
      class TagSerializer < ActiveModel::Serializer
        attributes :id, :name
      end
    end
    

    and now my JSON:

    {
       "tags": [
          {
             "id": "a33fc396-2428-11e3-8eeb-0800270f33f4",
             "name": "test"
          }
       ],
       "documents": [
          {
             "id": "c41460fa-2427-11e3-8702-0800270f33f4",
             "tag_ids": [
                "a33fc396-2428-11e3-8eeb-0800270f33f4"
             ]
          }
       ]
    }
    
    0 讨论(0)
提交回复
热议问题