0

我正在研究一种覆盖设计注册控制器的更新方法。

update_with_password 只是在其他地方使用单独的表单为用户更新密码工作正常。来到update_without_password时,日志可以看到它保持回滚(日志可以在下面看到)

Started PUT "/users" for 127.0.0.1 at 2013-08-30 10:45:03 +1000
Processing by RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxx", "user"=>{"name"=>"xxxxxx", "email"=>"xxx@gmail.com", "first_name"=>"xxx", "last_name"=>"xxx", "person_attributes"=>{"gender"=>"Unspecified", "description"=>"xxxxxx", "location"=>"xxx", "facebook"=>"https://www.facebook.com/xxxx", "website"=>"", "youtube"=>"", "wikipedia"=>"", "id"=>"11190"}}, "commit"=>"Save"}
User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 11190 LIMIT 1
User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 11190]]
(0.1ms)  BEGIN
**Person Load (0.5ms)  SELECT "people".* FROM "people" WHERE "people"."user_id" = 11190 LIMIT 1**
User Exists (0.5ms)  SELECT 1 AS one FROM "users" WHERE ("users"."name" = 'xxxxxx' AND "users"."id" != 11190) LIMIT 1
**(0.3ms)  ROLLBACK**
successfully_updated =======> false
CACHE (0.0ms)  SELECT "people".* FROM "people" WHERE "people"."user_id" = 11190 LIMIT 1
Rendered devise/registrations/edit.html.erb within layouts/application (16.5ms)

更新方法如下:

def update
  @user = User.find(current_user.id)

  successfully_updated = if needs_password?(@user, params)
    @user.update_with_password(params[:user])
  else
    params[:user].delete(:current_password)
    logger.info("USER params ====> #{params[:user]}")

    @user.update_without_password(params[:user])
  end
  logger.info("successfully_updated =======> #{successfully_updated}")  #######  THIS ALWAYS SHOWS "false"
  if successfully_updated
    .
    .(some extra code)
    .
  else
    render "edit"
  end
end

我已经检查了模型,用户有 person 的 accept_nested_attributes;Person 模型具有此表单的所有 attr_accessible。

知道我哪里有问题吗?我一直试图找出一点时间,但没有运气。谢谢你的时间。

4

1 回答 1

1

您应该检查您的模型设置,例如:accept_nested_attributes等,之前遇到过同样的问题,但真的不记得我做了什么来解决这个问题。

于 2013-08-30T04:09:01.640 回答