Rails Active Model Serializer未显示正确的属性

时间:2016-06-15 22:10:43

标签: ruby-on-rails

我不确定我在这里失踪了什么,有人能指出我正确的方向吗?我在模型中设置了初始化器和关联,但我的JSON仍然只返回Active记录中的属性,包括created_at和updated_at属性。我做错了什么?

ApplicationController.rb

class ApplicationController < ActionController::API
  include CanCan::ControllerAdditions
  include ActionController::Serialization



  acts_as_token_authentication_handler_for User

  rescue_from CanCan::AccessDenied do |exception|
    respond_to do |format|
      format.json { render nothing: true, status: :forbidden }

    end
  end
end

CompanyController.rb

def show
  @logo = Logo.find(params[:user_id])
  render json: @logo
end

CompanyModel.rb

class Company < ActiveRecord::Base
    belongs_to :user
    has_one :logo, dependent: :destroy
    has_many :cards, dependent: :destroy

end

LogoModel.rb

class Logo < ActiveRecord::Base
    belongs_to :company
    mount_uploader :image, ImageUploader
end

CompanySerializer.rb

module V1
    class CompanySerializer < ActiveModel::Serializer
      attributes :id, :user_id, :name

      has_one :logo
    end
end

LogoSerializer.rb

module V1
    class LogoSerializer < ActiveModel::Serializer
        attributes :id, :company_id, :image_url


    end
end

编辑:我应该发布CompanyController Action,而不是LogoController。我在上面添加了它并添加了LogoSerializer

0 个答案:

没有答案