Rails - AMS - Add a key for each json response from the controller

I am updating the Rails application to active_model_serializers 0.10.4, but I am having problems updating the necessary function: the ability to add request information to each json response from the controller.

In AMS 0.9.x, I used this using default_serializer_options , but this functionality has disappeared, and apparently the only way to achieve this manually adds a key metato EVERY request.

Has anyone found a workaround to do this job?

+4
source share
2 answers

in the config/initializers/active_model_serializer.rbfile add this line:

ActiveModel::Serializer.config.adapter = :json

@users data .

:

  def index
    @users = User.all
    render json: @users, root: "data"
  end

API JSON ,

ActiveModel::Serializer.config.adapter = :json_api

 def index
    @users = User.all
    @extra_meta = {"totalCount": @users.size}

    render json: @users, root: "data", meta: default_meta_attributes(@users, @extra_meta)
  end

default_meta_attributes current_user_id ..

+2

, ? ApplicationSerializer serialization_scope :view_context, , . JSON. , , SerializableResource .

0

Source: https://habr.com/ru/post/1668307/


All Articles