繁体   English   中英

Rails如何返回单个JSON对象而不是JSON对象数组?

[英]Rails how to return single JSON object instead of array of JSON objects?

我限制为1,因此我认为在这种情况下,它将简单地返回一个与.find_by_email相同的对象

码:

  # GET /users/:identified/type/:social_type
  # Returns a single record - so limit 1
  def find
    @user = User.where("identified = ? AND social_type = ?", params[:identified], params[:social_type]).limit(1)

    if not @user.empty?
      render json: @user.as_json, status: :created
    else
      render json: @user, status: :not_found
    end
  end

当前响应:

[{"id":7,"voy_num":null,"voy_pin":null}]

如何确保我返回单个JSON object

要获取单个对象,请first与以下where一起使用:

@user = User.where("identified = ? AND social_type = ?", params[:identified], params[:social_type]).first

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM