The root node of the active rail track serializer does not work

advertisements

I have a rails 4.2.5 api app. For some reason the json root node is not included in the response and I don't understand why.

curl  http://localhost:3000/api/v1/category/science

returns

{"title":"science","sub_categories":34}%

instead of

{"category": {"title":"science","sub_categories":34}%}

The code:

controller

class Api::V1::CategoryController < ApplicationController
  def show
    category = params[:category] || "sports"
    @category = Category.where(cat_title: category.capitalize).first
    respond_to do |format|
      format.json { render json: @category, serializer: CategorySerializer, root: "category" }
    end
  end
end

serializer

class CategorySerializer < ActiveModel::Serializer
  attributes :title, :sub_categories

  def title
    URI::encode(object.cat_title.force_encoding("ISO-8859-1").encode("utf-8", replace: nil).downcase.tr(" ", "_"))
  end

  def sub_categories
    object.cat_subcats
  end
end


have a look into your initializers, this should be commented out in wrap_paramters.rb:

# To enable root element in JSON for ActiveRecord objects.
# ActiveSupport.on_load(:active_record) do
#   self.include_root_in_json = true
# end