Rails设计更新没有密码,嵌套属性保持回滚

时间:2013-08-30 01:00:29

标签: ruby-on-rails-3 devise

我正在研究一种覆盖Devise RegistrationController的更新方法。

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)

Update方法如下:

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

我检查过模型,用户有人的accept_nested_attributes; Person模型具有此表单的所有attr_accessible。

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

1 个答案:

答案 0 :(得分:1)

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