Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 执行json put请求会将未编辑的字段保存为空_Ruby On Rails_Json - Fatal编程技术网

Ruby on rails 执行json put请求会将未编辑的字段保存为空

Ruby on rails 执行json put请求会将未编辑的字段保存为空,ruby-on-rails,json,Ruby On Rails,Json,在我的Rails应用程序中,我为iOS应用程序制作了一个api供其使用 我有一个与配置文件关联的用户模型: class User < ActiveRecord::Base has_one :personal_profile accepts_nested_attributes_for :personal_profile end class PersonalProfile < Profile belongs_to :user end 当我执行这样的put请求时: clas

在我的Rails应用程序中,我为iOS应用程序制作了一个api供其使用

我有一个与
配置文件关联的
用户
模型

class User < ActiveRecord::Base
  has_one :personal_profile
  accepts_nested_attributes_for :personal_profile
end

class PersonalProfile < Profile
  belongs_to :user
end
当我执行这样的put请求时:

class Api::V1::UsersController < Api::V1::ApiController
  before_filter :authenticate_api_user, only: [:show, :update]

  def create
    @user = User.new(user_params)
    if @user.save
      @user
    else
      render json: { errors: @user.errors.full_messages }, status: 422
    end
  end

  def show
    @user
  end

  def update
    if @user.update(user_params)
      @user
    else
      render json: { errors: @user.errors.full_messages }, status: 422
    end
  end

private

  def user_params
    params.require(:user).permit(:name, :email, :birthday, :gender, :password, :password_confirmation, personal_profile_attributes: [:website, :location, :description, :tagline, :tag_tokens, :image, :image_cache])
  end

  def authenticate_api_user
    authenticate_or_request_with_http_token do |token, options|     
      @user = User.find_by(auth_token: token)
    end
  end
end
curl -H 'Authorization: Token token=AUTH_TOKEN' -H 'Content-Type: application/json' -X PUT -d '{"user": {"name": "Frank", "personal_profile_attributes": { "tagline": "New tagline" }}}' http://localhost:3000/api/user
我得到的答复是:

{"errors":["Personal profile title can't be blank."]}
服务器好像不认识我的参数:

Started PUT "/api/user" for ::1 at 2016-01-19 10:16:33 +0100
Processing by Api::V1::UsersController#update as JSON
  Parameters: {"user"=>{"name"=>"Frank", "personal_profile_attributes"=>{"tagline"=>"New tagline"}}}
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."auth_token" = $1 LIMIT 1  [["auth_token", AUTH_TOKEN]]
   (0.8ms)  BEGIN
  PersonalProfile Load (1.2ms)  SELECT  "profiles".* FROM "profiles" WHERE "profiles"."type" IN ('PersonalProfile') AND "profiles"."user_id" = $1  ORDER BY title asc LIMIT 1  [["user_id", 1]]
  SQL (1.3ms)  UPDATE "profiles" SET "user_id" = $1, "updated_at" = $2 WHERE "profiles"."id" = $3  [["user_id", nil], ["updated_at", "2016-01-19 09:16:33.629907"], ["id", 1]]
  User Exists (1.3ms)  SELECT  1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('d@friis.me') AND "users"."id" != 1) LIMIT 1
   (0.8ms)  ROLLBACK
Completed 422 Unprocessable Entity in 19ms (Views: 0.3ms | ActiveRecord: 6.5ms)
我有另一个网络应用的控制器设置,这是相当标准的,工作刚刚好


非常感谢您的帮助

好的,问题是我没有为带有请求参数的关联记录传递
ID
。这意味着创建了一个新的关联记录,其中包含我试图更新的属性

设置
ID
解决了问题,并按预期更新了记录

{ 
  "user" => { 
    "personal_profile_attributes" => {
      "id" => 1,
      "tagline" => "New tagline"
    }
  }
}

看起来您的模型
Profile
已验证
validate\u是否存在:title
或类似的内容。Model
PersonalProfile
继承自
Profile
并具有与基本模型相同的验证。@是的,但
标题已设置,我不会在请求中更新它。可能是用户没有任何标题而保留的配置文件记录(例如,记录是在源代码中实现验证之前保存的)。rails控制台的
Profile.find(1).title
返回什么?