【发布时间】:2012-02-22 09:17:37
【问题描述】:
对于一个新的网络应用程序,我需要在我的注册页面(仅限管理员)上只有一个电子邮件字段。
问题是我对 Rails 完全陌生,所以即使是像这样的基础知识对我来说也非常困难......
我使用 Railscast #270 创建了我的身份验证,它使用 has_secure_password 方法。 现在,一切都很好,除了我不需要所有这些废话...... 我还想使用 Action Mailer 将生成的密码发送到他的电子邮件地址。 一个 hex(8) 密码将是完美的(我见过 SecureRandom 但它似乎已被贬值)
Users_Controller:
class UsersController < ApplicationController
skip_before_filter :is_connected?, :only => [:new, :create]
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
# Tell the Mailer to send a welcome Email after save
Mailer.confirm_email(@user).deliver
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end
end
end
用户模型:
class User < ActiveRecord::Base
attr_accessible :email
has_secure_password
validates_presence_of :password, :email, :on => :create
end
目前,在我看来,我有 2 个字段。但正如我之前所说,我只想要一个。 我想继续使用 has_secure_password ,它似乎为哈希/盐提供了很好的安全性。
【问题讨论】:
-
您的问题是“如何为我的用户设置密码并通过电子邮件发送给他们?”
-
基本上我想写一些会生成密码并将其设置为变量的东西,以便可以从我的邮件视图中访问,并且女巫将作为密码清单存储在我的数据库中。我不知道该怎么做。
-
好的,我照弗雷德里克说的做了。效果很好,但是有没有办法在不将 @random = SecureRandom.hex(8) 传递给视图的情况下做到这一点?因为我不知道该怎么做所以我将它作为隐藏字段传递给我的视图,然后我在我的创建方法中重用它......所以它的控制器>视图>控制器一无所有-_-
-
在控制器的create方法中,设置密码,然后传给用户,再传给user_mailer。
标签: ruby-on-rails random passwords