rails 3 + devise:如果注册表单有错误,如何防止在其他字段中擦除数据? [英] rails 3 + devise: if registration form has error, how prevent from erasing data in other fields?

查看:94
本文介绍了rails 3 + devise:如果注册表单有错误,如何防止在其他字段中擦除数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a custom field (referral_code) in my devise registration form. Also have a custom registration controller (so I can redirect to a custom "check your email" page after they register).

除非新用户错误信息(如密码/确认不匹配)显示错误信息时也会擦除引用代码字段。

It all works fine except if the new user makes a mistake (such as password/confirmation not matching) when the error message is displayed it also erases the referral code field.

如何获取有效数据(引用代码)另一个字段(密码或电子邮件)会向用户发回一个错误消息,留下填写?

How can I have valid data (referral code) stay "filled in" when another field (password or email) throws an error message back to the user?

推荐答案

将其添加为virtual_attribute。实际上,即使通过验证,您的自定义字段也不会被保存,您将会更加困惑。

You need to add this as a virtual_attribute. Actually, even if it passed the validation your custom field would not have saved and you would have been even more confused.

这是一个正常的设计帐户模型:

Here is a normal devise account model:

class Account < ActiveRecord::Base

    devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

    attr_accessible :email, :password, :password_confirmation, :remember_me

end

现在要获得自定义字段来保存,需要添加它们作为虚拟属性。

Now to get custom fields to save you need to add them as virtual attributes.

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 将保存。

这篇关于rails 3 + devise:如果注册表单有错误,如何防止在其他字段中擦除数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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