Ruby on Rails / Devise - 重设密码时,在模型中绕过自定义验证选项 [英] Ruby on Rails / Devise - Bypassing custom validation option in model when resetting password

查看:275
本文介绍了Ruby on Rails / Devise - 重设密码时,在模型中绕过自定义验证选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我修改了Devise的注册表,通过访问者添加一个:agree选项(用户必须接受注册服务条款等)。如果他们不同意,它不会验证。



当用户尝试编辑他们的帐户信息时,导致了一个问题,因为它认为:同意验证,但是我可以添加一个except子句,并添加了另一个访问器:在控制器中定义的signed_in(我无法弄清楚如何获取模型以确定用户是否已登录),设计人员的帮助在我的工作)。我的用户模型和users_controller的相关部分看起来像...



user.rb

 验证:agree::term_agreement => TRUE,:unless => :signed_in 

users_controller.rb

  def update 
@user = User.find(params [:id])

如果user_signed_in?
@ user.signed_in = params [:user]
end

[...]
end

所以,这一切都很好...当用户已经登录时,同意验证将覆盖。但是,我必须找出最好的方法覆盖另一种情况...用户重置密码并需要更改密码。



我正在测试用户帐户,并尝试在一个帐户上重置我的密码,但是被击中:同意验证...现在我必须找出一种方法来重写。我注意到更改您的密码表单有一个隐藏的字段值:reset_password_token,但是我尝试:除非=>:reset_password_token但是不起作用。



那么什么是完成这个最好的方法?除此之外,我如何可以使用/或条件(除非:signed_in或:reset_password等):except子句?

解决方案

所以,创建记录时需要验证条款的接受度?

  class User< ActiveRecord :: Base 
验证:agree,:acceptance => true,on => :创建
end

:on => :创建只会在创建记录时执行该验证,就像布兰登的答案一样,但没有冗余代码。



这也将避免您的控制器需要担心用户是否登录。


In my project, I altered the registration form in Devise to add an :agree option via an accessor (user must accept terms of service to register, etc). If they don't agree, it doesn't validate.

That caused a problem when a user tried to edit their account information as it seeked out the :agree validation, but I was able to add an 'unless' clause and added another accessor called :signed_in that is defined in the controller (I couldn't figure out how to get the model to determine if the user was signed in or not, devise's helpers wouldn't work for me in it). The relevant portions of my User model and users_controller look like...

user.rb

validates :agree, :term_agreement => TRUE, :unless => :signed_in

users_controller.rb

def update
   @user = User.find(params[:id])

   if user_signed_in?
     @user.signed_in = params[:user]
   end

  [...]
end

So, it all works fine...the "agree" validation overrides when a user is already signed in. However, I have to figure out the best way to override another scenario...when a user resets their password and needs to change it.

I was testing user accounts and tried to reset my password on one account, however I was hit with the :agree validation...now I have to figure out a way to override that. I noticed the Change your password form has a hidden field value of :reset_password_token, however I tried :unless => :reset_password_token but it wouldn't work.

So what is the best way of accomplishing this? On top of that, how can I have an either / or condition (unless :signed_in or :reset_password, etc) for that :unless clause?

解决方案

So, you need to validate the acceptance of terms when the record is created?

class User < ActiveRecord::Base
  validates :agree, :acceptance => true, :on => :create
end

:on => :create will only perform that validation when the record is being created—much like Brandon's answer, but without redundant code.

This will also obviate the need for your controllers to worry about if a user is signed in or not.

这篇关于Ruby on Rails / Devise - 重设密码时,在模型中绕过自定义验证选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆