ruby-on-rails – 随机生成的密码Rails 3.1
发布时间:2020-12-16 20:03:10 所属栏目:百科 来源:网络整理
导读:为了一个新的网络应用程序的目的,我需要在我的注册页面(仅限管理员)只有一个电子邮件字段. 事情是,在轨道上完全是新的,所以甚至基本的东西,对我来说真的很困难… 我使用Railscast#270创建了我的身份验证,使用has_secure_password方法. 现在,一切工作都很好,
为了一个新的网络应用程序的目的,我需要在我的注册页面(仅限管理员)只有一个电子邮件字段.
事情是,在轨道上完全是新的,所以甚至基本的东西,对我来说真的很困难… 我使用Railscast#270创建了我的身份验证,使用has_secure_password方法. 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 User_model: class User < ActiveRecord::Base attr_accessible :email has_secure_password validates_presence_of :password,:email,:on => :create end 现在,在我看来,我有2个字段.但正如我刚才所说,我只想要一个. 解决方法
Rails提供ActiveSupport :: SecureRandom(根据Ruby版本)只是Ruby的SecureRandom的桥梁,或者在旧版本的Ruby上重新实现它(如果我的内存正确,SecureRandom在1.8.7中被添加)
现在,Rails支持的所有Ruby版本都已经不再需要SecureRandom内置的ActiveSupport :: SecureRandom,并已被弃用. SecureRandom本身无处可去 – require 'securerandom' SecureRandom.hex(8) 应该做的很好(你可能想考虑SecureRandom.urlsafe_base64更紧凑的表示相同数量的实际随机性) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |