Ruby on rails 如何在JSON中公开继承_列
我需要在JSON响应中公开继承_列,以便在前端(Angular)应用程序中检索它。我怎样才能把它变成现实 我已经寻找了很多关于这个问题的答案,但是我没有找到 在我的案例场景中,我的用户可以是管理员、员工或客户。唯一的区别是Customer比Admin和Employee多了两个字段。这就是我决定实施STI的原因。如果我做出了错误的选择,请随时告诉我 my user_serializer.rb的示例:Ruby on rails 如何在JSON中公开继承_列,ruby-on-rails,activerecord,ruby-on-rails-5,sti,Ruby On Rails,Activerecord,Ruby On Rails 5,Sti,我需要在JSON响应中公开继承_列,以便在前端(Angular)应用程序中检索它。我怎样才能把它变成现实 我已经寻找了很多关于这个问题的答案,但是我没有找到 在我的案例场景中,我的用户可以是管理员、员工或客户。唯一的区别是Customer比Admin和Employee多了两个字段。这就是我决定实施STI的原因。如果我做出了错误的选择,请随时告诉我 my user_serializer.rb的示例: class UserSerializer < ActiveModel::Serializer
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :email, :type
end
class UserSerializer
My user.rb位于/models中
My users_controllers.rb位于controllers/api/v1中/
更新:
我的控制器:
class Api::V1::UsersController < Api::V1::BaseApiController
before_action :authenticate_user!
# some methods...
def show
user = User.find(params[:id])
if user.present?
render json: { data: user }, status: 200
else
head 404
end
end
# some methods...
private
def user_params
params.require(:user).permit(
:id,
:name,
:email,
:password, :password_confirmation,
:registration,
:cpf,
:landline, :cellphone, :whatsapp,
:simple_address,
:public_agency_id,
:public_office_id,
:type
)
end
end
class Api::V1::UsersController
我的模型:
class User < ApplicationRecord
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
include DeviseTokenAuth::Concerns::User
# belongs_to :address
attr_accessor :skip_password_validation
validates :name, presence: true
validates :type, presence: true
scope :admins, -> { where(type: 'Admin') }
scope :employees, -> { where(type: 'Employee') }
scope :customers, -> { where(type: 'Customer') }
# CALLBACKS
before_validation :generate_uuid!
before_create :downcase_email
def password_required?
return false if skip_password_validation
super
end
def token_validation_response
{
id: id,
email: email,
name: name,
surname: surname,
cpf: cpf,
landline: landline,
cellphone: cellphone,
whatsapp: whatsapp,
simple_address: simple_address,
created_at: created_at,
updated_at: updated_at,
type: type
}
end
private
def generate_uuid!
self.uid = SecureRandom.uuid if self.uid.blank?
end
def downcase_email
self.email = self.email.delete(' ').downcase
end
end
class用户{where(类型:'Admin')}
作用域:employees,->{where(type:'employeer')}
作用域:客户,->{where(类型:'Customer')}
#回调
验证前:生成\u uuid!
创建前:下载电子邮件
是否需要def密码?
如果跳过密码验证,则返回false
超级的
结束
def令牌验证响应
{
id:id,
电邮:电邮,,
姓名:姓名,,
姓:姓,,
中央公积金,
座机:座机,
手机:手机,
whatsapp:whatsapp,
简单地址:简单地址,
创建时间:创建时间,
更新日期:更新日期:,
类型:类型
}
结束
私有的
def生成_uuid!
如果self.uid.blank,self.uid=SecureRandom.uuid?
结束
def downcase_电子邮件
self.email=self.email.delete(“”).downcase
结束
结束
响应对象示例:
关于config/initializers/active_model_serializer.rb,我的项目中没有这样的文件。我想我们需要更多关于控制器、模型、初始化器和JSON输出的信息来准确诊断这个问题。但我会在黑暗中拍摄,这可能会有所帮助 总的来说,看起来您可能没有加载UserSerializer。我建议将它移到序列化程序的根目录中,并在那里工作:然后,如果出于某种原因想将它移到api/v1,您可以将注意力集中在名称空间问题上,这可能会导致问题的解决
# app/serializers/user_serializer.rb
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :email, :type
end
您可能还需要更新应用程序控制器以进行序列化:
class ApplicationController < ActionController::API
include ActionController::Serialization
# ...
end
class ApplicationController
一些可能有用的资源:
您只是在使用默认的继承列“type”吗?还是定义了唯一的列?为什么不直接将“type”添加到.jbuilder视图输出中呢。是的,我使用的是默认的comun:type。我从来没有使用过jbuilder,我使用的是gem active\u model\u序列化程序,但它不起作用,我的用户\u serializer.rb被忽略了。你能同时发布你的控制器和模型吗?另外,你有
配置/初始化器/active\u model\u serializer.rb
?如果是这样的话,请在tooLastly上发布,您能输出缺少类型字段的JSON api调用的结果吗?这可能有助于诊断是否使用ActiveModelSerializer呈现JSON=/
class ApplicationController < ActionController::API
include ActionController::Serialization
# ...
end