简体   繁体   English

设计.skip_confirmation! 没有保存

[英]Devise .skip_confirmation! is not saving

I have added a custom method to my (Devise) User controller: 我向(Devise)用户控制器添加了一个自定义方法:

def confirm
    @user = User.find(params[:id])
    @user.skip_confirmation!

    respond_to
        if @user.save 
            //display success message
        else
           // display error message
        end
    end
end

When I try to confirm a user via that, the save fails and I have two errors in @user.errors: password and password_confirm 当我尝试通过该方法确认用户时,保存失败,并且@ user.errors中出现两个错误:password和password_confirm

Even in the rails console, if I try 即使在Rails控制台中,如果我尝试

@user.skip_confirmation!
@save

I get a rollback and the save fails. 我回滚,保存失败。

Any ideas would be appreciated.. 任何想法,将不胜感激..

Additional Info 附加信息

The problem causing this is the 造成此问题的原因是

validates_presence_of :password, :password_confirmation

in my User model. 在我的用户模型中。 Even though a password and password confirmation was entered by the user on signup, those get hashed together to make the encrypted_password. 即使用户在注册时输入了密码和密码确认,这些密码和哈希密码也会被散列在一起以生成crypted_pa​​ssword。 For example, when I find a user in the console, the user has an encrypted_password, but no password or password_confirmation. 例如,当我在控制台中找到一个用户时,该用户有一个cryptomed_pa​​ssword,但是没有password或password_confirmation。

I have tried this 我已经试过了

validates_presence_of :password, :password_confirmation, :on => :create

However it doesn't force the user to enter both a password and a confirm on the Change Password screen. 但是,这不会强制用户在“更改密码”屏幕上输入密码和确认。

Any idea on how to get around this? 关于如何解决这个问题的任何想法?

I think you're misunderstanding what skip_confirmation! 我认为您误会了skip_confirmation! does. 做。

Devise's confirmable module will send a new user an e-mail with a link back to your app. Devise的confirmable模块将向新用户发送一封电子邮件,其中包含指向您应用程序的链接。 Until they click that link, they'll be "unconfirmed" and unable to sign in. What skip_confirmation! 直到他们点击该链接,他们将是“未经证实”,并无法登录。什么skip_confirmation! does is skip this workflow -- the user will be immediately confirmed, and can log in without first having to go through the e-mail confirmation flow. 这样做是跳过该工作流程-用户将立即得到确认,并且无需先通过电子邮件确认流程即可登录。

You're getting the error you're getting because of validations on the user model. 由于用户模型上的验证,您遇到了错误。 In particular, the user you're trying to save doesn't appear to have an existing password, so it's requiring a password and matching password_confirmation attribute. 特别是,您要保存的用户似乎没有现有密码,因此需要password和匹配的password_confirmation属性。

I suspect that there's a better way to accomplish whatever your purpose is. 我怀疑,无论您的目标是什么,都有更好的方法来实现。 Why isn't the normal confirmation workflow sufficient? 正常的确认工作流程为什么还不够? What's this extra controller action trying to accomplish? 这个额外的控制器动作想完成什么?

I think skip_confirmation! 我认为skip_confirmation! actually does the saving. 真正做到了节省。

What you want to check is probably if @user.persisted? 您想要检查的可能if @user.persisted?

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

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