【问题标题】:Editing model with Ember-data and Rails not saving使用 Ember-data 和 Rails 编辑模型不保存
【发布时间】:2015-04-11 21:22:33
【问题描述】:

我正在尝试使用 ember-data 和 rails 编辑用户,当我编辑用户信息并尝试执行 model.save() 时,我得到一个

unexpected end of input

我的控制台出错。 看起来 Rails 端正在进行一个奇怪的查询。这是来自 Rails 的查询:

Started PUT "/users/2" for ::1 at 2015-04-11 15:59:17 -0500
 Processing by UsersController#update as HTML
 Parameters: {"user"=>{"username"=>"adminUser", "email"=>"admin@test.com", "password"=>"[FILTERED]", "admin"=>true}, "id"=>"2"}
 User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 2]]
 ApiKey Load (0.4ms)  SELECT  "api_keys".* FROM "api_keys" WHERE (expired_at >= '2015-04-11 20:59:17.113883') AND "api_keys"."access_token" = $1  ORDER BY "api_keys"."id" ASC LIMIT 1  [["access_token", "500b9b5d38ecb9f851b89823d5b40d0f"]]

 User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 2]]

 ApiKey Load (0.4ms)  SELECT  "api_keys".* FROM "api_keys" WHERE "api_keys"."user_id" = $1 AND (expired_at >= '2015-04-11 20:59:17.117827')  ORDER BY "api_keys"."id" ASC LIMIT 1  [["user_id", 2]]
 (0.2ms)  BEGIN
 User Exists (0.6ms)  SELECT  1 AS one FROM "users" WHERE (LOWER("users"."username") = LOWER('adminUser') AND "users"."id" != 2) LIMIT 1
 User Exists (0.3ms)  SELECT  1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('admin@test.com') AND "users"."id" != 2) LIMIT 1
 (0.2ms)  ROLLBACK

我不明白为什么 Rails 会像这样尝试选择 !=2。

【问题讨论】:

    标签: ruby-on-rails ruby ember.js ember-data active-model-serializers


    【解决方案1】:

    有些验证失败。您正在更新 id 为 2 的用户,我打赌您在 usernameemail 上有唯一性验证。问题是有另一个User 记录至少具有相同的电子邮件地址,这导致了回滚。

    您可能还应该确保在更新失败时返回状态 422 (Unprocessable Entity) 并带有适当的错误。

    【讨论】:

    • 谢谢!这实际上不是任何验证错误,但 Ember 正在尝试发送密码 nil
    • 这样就行了!很高兴您发现了问题。密码存在也是一种验证。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2013-05-09
    • 2023-03-07
    • 1970-01-01
    • 2014-03-18
    • 2014-02-14
    相关资源
    最近更新 更多