Ruby on rails 铁路及;设计:电子邮件确认文本字段?
我正在使用Rails 3.2和Desive。我想知道Deave是否提供了一个“电子邮件(地址)确认”字段,就像密码确认一样,让用户在网站发送确认电子邮件或处理注册之前键入密码字段,然后键入密码确认字段。如果没有,我必须添加电子邮件确认并验证用户模型(在rails g Desive User之后)Ruby on rails 铁路及;设计:电子邮件确认文本字段?,ruby-on-rails,devise,Ruby On Rails,Devise,我正在使用Rails 3.2和Desive。我想知道Deave是否提供了一个“电子邮件(地址)确认”字段,就像密码确认一样,让用户在网站发送确认电子邮件或处理注册之前键入密码字段,然后键入密码确认字段。如果没有,我必须添加电子邮件确认并验证用户模型(在rails g Desive User之后) 提前感谢假设您安装了gem,执行此功能时: rails generate devise:install 它将生成您的迁移文件,其中包含您的电子邮件确认内容,您需要按照您所指的方向进行。你所要做的就是不
提前感谢假设您安装了gem,执行此功能时:
rails generate devise:install
它将生成您的迁移文件,其中包含您的电子邮件确认内容,您需要按照您所指的方向进行。你所要做的就是不要评论它。否则,我会注释掉所有其他我不需要的东西
您所指的模块称为可确认模块
假设您尚未找到此链接,此链接将解释其余内容:
我没有发现Deave是否按照您的要求执行(问题是指第二个:电子邮件确认字段类似于:密码确认,“可确认”似乎仅与确认链接相关)。我也必须这样做,并且我发现了如何使用内置的活动记录验证(rails 3.2.8,Desive 2.1.2)来实现这一点:
确认:true
添加到您的电子邮件验证中,并将确认电子邮件添加到attr\u accessible
:email\u confirmation
字段<div><%= f.label :email_confirmation %>
<%= f.email_field :email_confirmation %></div>
也许有一个更干净的方法,但这对我很有效。然而,到目前为止,我对Desive的唯一体验是将其添加到现有模型中,我在为您生成模型时没有使用过它(可能基本相同)。我也面临同样的问题 以下是我解决问题的方法 当有两个文本字段接收完全相同的内容时,应该使用此帮助器。例如,您可能需要确认电子邮件地址或密码。此验证将创建一个虚拟属性,其名称是必须附加“\u confirmation”进行确认的字段的名称
class Person < ApplicationRecord
validates :email, confirmation: true
end
还有一个:区分大小写的
选项,可用于定义确认约束是否区分大小写。此选项默认为true
class Person < ApplicationRecord
validates :email, confirmation: { case_sensitive: false }
end
class-Person
此帮助程序的默认错误消息为“与确认不匹配”
根据我的实验,使用
如果要将确认消息更改为“必须正确发送电子邮件确认”,请按以下方式使用添加消息选项:
class Person < ApplicationRecord
validates :email, confirmation: true
validates :email_confirmation, presence: { message: "must be given correctly" }
end
class-Person
就这些
我希望这有帮助较新版本的Rails与原始答案相比有一些细微的差异。我想我会加上这个,以防对其他人有帮助 向您的模型中添加一个
attr\u访问器,用于电子邮件确认,并对照电子邮件确认字段验证电子邮件字段。除非已将电子邮件字段配置为区分大小写,否则在验证中将区分大小写设置为false{case\u-sensitive:false}
app/models/user.rb
class用户
允许在注册参数上通过电子邮件确认
app/controller/users/registrations\u controller.rb
def配置\注册\参数
设计参数消毒器。许可证(:注册,钥匙:[:电子邮件确认])
结束
将电子邮件确认字段添加到您的注册表单中
app/views/designe/registrations/new.html.erb
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render 'devise/shared/error_messages', resource: resource %>
<div class="field">
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, autocomplete: 'email', required: true %>
</div>
<div class="field">
<%= f.label :email_confirmation %>
<%= f.email_field :email_confirmation, required: true %>
</div>
<!-- other fields -->
<% end %>
这并不能回答他的问题。他正在注册表单中寻找一个:email\u确认字段,该字段的工作原理与:password\u确认字段类似。恐怕这个答案不正确;“可确认”模块发送电子邮件,要求用户单击链接确认帐户后才能使用,提问者询问是否可以自动添加第二个电子邮件字段,该字段必须与第一个字段匹配,才能通过验证。
class Person < ApplicationRecord
validates :email, confirmation: { case_sensitive: false }
end
class Person < ApplicationRecord
validates :email, confirmation: true
validates :email_confirmation, presence: { message: "must be given correctly" }
end
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render 'devise/shared/error_messages', resource: resource %>
<div class="field">
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, autocomplete: 'email', required: true %>
</div>
<div class="field">
<%= f.label :email_confirmation %>
<%= f.email_field :email_confirmation, required: true %>
</div>
<!-- other fields -->
<% end %>