簡體   English   中英

ActiveModel序列化器不適用於該功能

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM