1..确保模型中包含可确认的内容.设计呼叫
class User < ActiveRecord::Base
devise :database_authenticatable, :confirmable ...
end
2.确保将confirmable添加到用户迁移中
create_table :users do |t|
t.database_authenticatable
t.confirmable
...
end
如果您使用的是Deviate 2.0+,则会失败,因为Deviate不再提供迁移助手,因此t.confirmable
会引发错误.相反,从their migration guide复制标记为"Confirmable"的块.
3.使用以下任一命令生成设备视图,以便您可以覆盖设备邮件程序视图:
rails generate devise:views # global
rails generate devise:views users # scoped
您现在可以覆盖devise/mailer/confirmation_instructions.html.erb
或users/mailer/confirmation_instructions.html.erb
中的邮件视图,具体取决于您的设置
4.对于development环境,在/config/environments/development.rb
中添加以下配置行
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {:address => "localhost", :port => 1025}
5.对于/config/environments/production.rb
中的production环境,您可以使用类似于以下内容的内容(假设您在本地主机上有一个SMTP服务器:25):
config.action_mailer.default_url_options = {:host => 'yourdomain.com'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "127.0.0.1",
:port => 25,
:domain => 'yourdomain.com'
}
6要测试开发中的设置,请安装mailcatcher gem,您将在开发中用作SMTP服务器,捕获所有传入邮件并在http://localhost:1080/
上显示它们:
gem install mailcatcher
安装后,使用以下命令启动mailcatcher服务器:
mailcatcher
一个玩具SMTP服务器将在端口1025上运行,捕捉邮箱并在HTTP端口1080上显示它们.
You can now create an account and see the confirmations.