在acts_as_authenticated里使用密码找回功能

本文详细介绍了如何使用Rails框架实现简单的邮件发送功能,包括邮件发送器的配置、邮件模板的设计、用户密码重置流程的实现等步骤。通过生成迁移文件、修改模型类方法、创建观察者等方式,确保邮件发送功能的正确性和用户体验。
虽然不喜欢这个插件,理由嘛,太老了...但还是要用,过段时间有机会再改吧

具体配置什么之类的,参考前一篇:
用rails实现简单邮件发送测试
[url]http://fireflyman.iteye.com/blog/800955[/url]

(1)
ruby script/generate authenticated_mailer user


(2)删除models/user_notifier 里的两个方法:UserNotifier和activation
然后折腾后的代码是这样的--<

class UserNotifier < ActionMailer::Base
@@session = ActionController::Integration::Session.new
#因为url_for是ActionController类控制器的实例方法,所以不能在邮件发送器中直接调用.
#因此我们创建了一个可以调用url_for的ActionController::Integration::Session对象, #并赋值给一个类变量,这样就可以在邮件发送器中任意使用url_for 了
def forgot_password(user)
setup_email(user)
@subject += "Password reset"
@body[:url] = @@session.url_for(:controller => "account",
:action => "reset_password",
:id => user.pw_reset_code,
:only_path => false )
end

protected
def setup_email(user)
@subject = "hello World"
@sent_on = Time.now
@body[:user] = user
@recipients = "#{user.email}"
@from = 'xxx@163.com'
@headers = {}
end
end


(3)在views/user_notifier里创建对应的邮件模板,我的如下==>
Dear:<%= @user.login %>

请点击下面链接地址,以便重设密码:
<%= @url %>


(4)为users表增加pw_reset_code字段,目的是存放一个随机生成的记号,需要把这个记号发给用户.
ruby script/generate migration add_pw_reset_code_to_user


class AddPwResetCodeToUsers < ActiveRecord::Migration
def self.up
add_column :users, :pw_reset_code, :string, :limit => 40
end

def self.down
remove_column :users, :pw_reset_code
end
end


(5) 打开你的user.rb,增加下面两个类方法
 def forgot_password
self.password_forgotten = true
create_pw_reset_code
end

def reset_password
update_attributes(:pw_reset_code => nil )
end

protected

def create_pw_reset_code
self.pw_reset_code = Digest::SHA1.hexdigest("secret-#{Time.now}")
end


(6)打开models/user_observer
class UserObserver < ActiveRecord::Observer

def after_save(user)
UserNotifier.deliver_forgot_password(user) if user.password_forgotten
end

end


(7)在environment.rb里加入这句==?
config.active_record.observers = [:user_observer]


(8)在account_controller里加入下面两个方法:
def forgot_password
return unless request.post?
if @user = User.find_by_email(params[:email])
@user.forgot_password
@user.save
flash[:notice] = "An email with instructions for resetting your password has been sent to your email address."
redirect_back_or_default(:controller => "/account")
else
flash.now[:notice] = "Could not find a user with the given email address."
#render :forgot_password
end
end

def reset_password
@page_title = "Reset Password"
@user = User.find_by_pw_reset_code(params[:id]) rescue nil
unless @user
render(:text => "Not found",:status => 404)
return
end
return unless request.post?
if @user.update_attributes(params[:user])
@user.reset_password
flash[:notice] = "Password successfully reset."
redirect_back_or_default(:controller => "/account")
end


(9)到这一步已经没啥好做了,无非就设计两个页面-->自己慢慢改...
forgot_password.rhtml和reset_password.rhtml

<p>Give you email address and we'll send your instructions on how tou create a new one.</p>

<% form_for :user do |f| %>
<p><label for="email">Email:</label><br/>
<%= text_field_tag :email %><br />
<%= submit_tag "Submit" %>
<% end %>



<%= error_messages_for :user %>
<% form_for :user do |f|%>
<p>Password:<br/>
<%= password_field :user,:password %></p>
<p>Confirm password:<br/>
<%= password_field :user,:password_confirmation %></p>
<p><%= submit_tag "Submit"%></p>
<% end %>


好了,经测试,是可以的..这只是一个笔记记录,用上就用,用不上别骂..
内容概要:本文详细介绍了一种基于Simulink的表贴式永磁同步电机(SPMSM)有限控制集模型预测电流控制(FCS-MPCC)仿真系统。通过构建PMSM数学模型、坐标变换、MPC控制器、SVPWM调制等模块,实现了对电机定子电流的高精度跟踪控制,具备快速动态响应和低稳态误差的特点。文中提供了完整的仿真建模步骤、关键参数设置、核心MATLAB函数代码及仿真结果分析,涵盖转速、电流、转矩和三相电流波形,验证了MPC控制策略在动态性能、稳态精度和抗负载扰动方面的优越性,并提出了参数自整定、加权代价函数、模型预测转矩控制和弱磁扩速等优化方向。; 适合人群:自动化、电气工程及其相关专业本科生、研究生,以及从事电机控制算法研究与仿真的工程技术人员;具备一定的电机原理、自动控制理论和Simulink仿真基础者更佳; 使用场景及目标:①用于永磁同步电机模型预测控制的教学演示、课程设计或毕业设计项目;②作为电机先进控制算法(如MPC、MPTC)的仿真验证平台;③支撑科研中对控制性能优化(如动态响应、抗干扰能力)的研究需求; 阅读建议:建议读者结合Simulink环境动手搭建模型,深入理解各模块间的信号流向与控制逻辑,重点掌握预测模型构建、代价函数设计与开关状态选择机制,并可通过修改电机参数或控制策略进行拓展实验,以增强实践与创新能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值