rails 3 + devise:如果注册表有错误,如何防止其他字段中的数据被删除?

我的设计注册表中有一个自定义字段(referral_code)。 还有一个自定义注册控制器(所以我可以在注册后重定向到自定义“检查您的电子邮件”页面)。

一切正常,除非新用户在显示错误消息时出错(例如密码/确认不匹配),它也会删除推荐代码字段。

当另一个字段(密码或电子邮件)向用户发送错误消息时,如何保持“填写”有效数据(推荐代码)?

您需要将其添加为virtual_attribute。 实际上,即使它通过了validation,你的自定义字段也不会保存,你会更加困惑。

这是一个普通的设计帐户模型:

class Account < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email, :password, :password_confirmation, :remember_me end 

现在要获取自定义字段以保存,您需要将它们添加为虚拟属性。

 class Account < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email, :password, :password_confirmation, :remember_me, :phone_number, :other_field end 

现在:phone_number, :other_field将保存。