首页 > 解决方案 > 试图从一个类继承并在其上添加一个属性,得到一个 TypeError

问题描述

class Api::ApiResponse
  attr_accessor :success, :errors
  def initialize(success, errors)
    @success = success
    @errors = errors
  end
end


class NewSessionResponse < Api::ApiResponse
  attr_accessor :user
end

在我的控制器动作中,我有这个:

ar = NewSessionResponse.new(true, [])

ar.user = user
render json: ar

出于某种原因,我收到此错误:

TypeError (superclass mismatch for class NewSessionResponse)

当我在 Rails 控制台中运行此代码时,它工作正常。

顺便说一句,父类的构造函数被传递给子类?

标签: ruby-on-railsruby

解决方案


推荐阅读