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

查看:124
本文介绍了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天全站免登陆