как удалить автогенерируемый JSON несущей волны

Я использую jquery-file-upload для загрузки файла изображения аватара в носитель с помощью rails.

Контроллер:

class Api::V1::AvatarsController < ApplicationController
  load_and_authorize_resource :user
  load_and_authorize_resource :avatar, through: :user
  respond_to :json

  def create
    @avatar.active = true
    @avatar.save
    respond_with @avatar, location: nil
  end
end

Я также использую active_model_serializers для генерации json:

class AvatarSerializer < ApplicationSerializer
  attributes :active
  attributes :image
  attributes :crop_x
  attributes :crop_y
  attributes :crop_w
  attributes :crop_h

  attributes :image_medium_path
  attributes :image_large_path
  attributes :can_manage

  attributes :user_id

  def image_medium_path
    object.image_url :medium
  end

  def image_large_path
    object.image_url :large
  end

  def can_manage
    Ability.new(scope).can?(:manage, object)
  end
end

После того, как я загрузил изображение аватара с помощью jquery-file-upload, я получил ответ:

  {"avatar":{
      "id":"51eba3143803f8aa7c000009",
      "active":true,
      *"image":{
        "image":{
          "url":"...",
          "large":{
            "url":"..."
          },
          "medium":{
            "url":"..."
          },
          "small":{
            "url":"..."
          },
          "xsmall":{
            "url":"..."
          },
          "xxsmall":{
            "url":"..."
          }
        }
      },*       "crop_x":0,"crop_y":0,"crop_w":294,"crop_h":294,"image_medium_path":"...","image_large_path":"...","can_manage":true,"user_id":"51e4156a3803f8cde900000b"}}

Как вы можете видеть, раздел «изображение» json является добавленной частью carriverwave. Я хочу удалить его. как это сделать? Спасибо


person xnjiang    schedule 21.07.2013    source источник


Ответы (1)


Я только что решил аналогичную проблему, переопределив метод to_json.

class MyModel < ActiveRecord::Base
  ...
  def to_json(options = nil)
    params = {field: self.field,... image: self.image.url}
    ActiveSupport::JSON.encode(params, options)
  end
end
person user1610127    schedule 05.09.2013
comment
Спасибо за Ваш ответ! - person xnjiang; 06.09.2013
comment
У меня та же проблема с использованием драгоценного камня active_model_serializers. Вы знаете, что я могу сделать? - person William Weckl; 04.09.2014