[英]ActiveModel Serializer not working for function
我在Rails API中使用ActiveModelSerialziers ,它的工作原理像一个超级按钮,但我的其中1个函数似乎将其覆盖,并且仅返回我的模型(不包含任何关联)。 如果调用了不同的函数,则相同的模型将正确返回。
def getClient
type = params[:type]
if type == 'user'
@client = Client.find_by(user_id: params[:id])
else
@client = Client.find_by(id: params[:id])
end
render json: { success: true, response: @client }
end
仅返回没有关联的client
,并且序列化器为:
class API::ClientSerializer < ActiveModel::Serializer
attributes :name, :age, :address
belongs_to :user
has_many :check_ins
has_many :client_docs
has_many :payments
end
当调用以下函数时,同一个模型( Client
)正确返回(带有关联):
def show
model_name = params[:model].classify
item = model_name.constantize.find_by(id: params[:id])
render json: item, status: :ok
end
为什么会发生这种情况?
您在rander语句中缺少适配器::json:
render json: item, adapter: :json, status: :ok
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.