Rails,DEVISE - 防止用户更改他们的电子邮件地址 [英] Rails, DEVISE - Preventing a user from changing their email address

查看:17
本文介绍了Rails,DEVISE - 防止用户更改他们的电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户在我的应用上注册时,他们必须确认他们的电子邮件,由 Devise + Rails 3 提供支持.

When a user registers on my app they have to confirm their email, powered by Devise + Rails 3.

电子邮件地址定义了用户的权限,因此我不希望用户在注册后就可以更改它.因此删除了:来自 users.rb attr_accessible 的电子邮件,该电子邮件适用于已登录用户,但现在用户无法注册.

The email address defines the user's permissions so I don't want the user to be able to change it once registered. so removed :email from the users.rb attr_accessible which worked for a logged in user, but now user's can't register.

处理这个问题的正确方法是什么?因此用户无法更新他们的电子邮件,但可以使用 devise 使用他们的电子邮件进行注册.

What's the right way to handle this? So users can't update their email but can register with their email using devise.

谢谢

推荐答案

这是自定义验证器的完美案例.从 Rails3 开始,它们比以前容易多了.

This is the perfect case for a custom validator. Since Rails3, they are much easier to do than before.

class ImmutableValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    record.errors[attribute] << "cannot be changed after creation" if record.send("#{attribute}_changed?") && !record.new_record?
  end
end

class User < ActiveRecord::Base
  validates :email, :immutable => true
end

这篇关于Rails,DEVISE - 防止用户更改他们的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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