Gem devise 用 username 作为验证的问题

yfeng · February 03, 2012 · Last by yfeng replied at February 04, 2012 · 4185 hits

一个应用中,前后台都用 devise 作为验证,前台是用的默认的 email,后台用 username,按照官方文档: Another way is to change your model: devise :database_authenticatable, :authentication_keys => [:] And then make the field accessible: attr_accessible :

加了 :authentication_keys =>[:username] 和 attr_accessible :username

数据库字段和 new.html.erb 都把 相应的 email 改为 username 了,但是注册时候提交就报什么 email 不是一个方法这种错误,哪位遇到过,帮忙看看。谢谢

你用的神马版本?2.0 么? 如果是,2.0 不包含 migration 的 help 了,需要自己明确表结构

https://github.com/plataformatec/devise/wiki/How-To:-Upgrade-to-Devise-2.0-migration-schema-style

@lainuo 请问这个跟迁移有什么关系?我不是 2.0 我要实现用 username 来注册而不是 email.但是一直都不行,始终提示 undefined email method..NND..email 啥时候变方法了..唉..换个字段咋都这麻烦呢..

你把 email 从 attr_accessible 里拿掉了当然没有了。。

@ywencn 如果不拿掉,我注册表单保存的时候,没有 EMAIL 字段了,会提示 EMAIL 不能为空,问题是我想用 USERNAME 代替 EMAIL 字段啊

#4 楼 @yfeng 你先看看 model 的定义,把相关的校验去掉,就不会有类似不能为空的提示了。

提示 EMAIL 不能为空 这个问题好解决,是因为表中有 email 字段,页面上没有,如果把相关配置都改成 username,在表里干掉 email,那么就直接报 undefined email method..已经不用 email 字段来验证了还报这个。。

You need to Sign in before reply, if you don't have an account, please Sign up first.