之前写过密码找回的,账号激活的也一起写吧...下面的代码是在之前那个的基础上改的...
在acts_as_authenticated里使用密码找回功能
http://fireflyman.iteye.com/blog/801953
首先,请不要再问我为什么不使用
restful_authentication这个插件,一句命令就解决了...
ruby script/generate authenticated user sessions --include-activation
而且有现成的例子...
用Rails 2.3打造简单记账软件(3)
http://dohkoos.name/use-rails-23-to-create-a-simple-accounting-app-3.html
反正就一言难尽..非要说一句的话,我只能说:火星文,你妹啊...
废话就不说了,开始吧:
1.为Users表增加两个字段activation_code和activated_at
ruby script/generate migration AddActivationCodeToUser activation_code:string activated_at:datetime
class AddActivationCodeToUser < ActiveRecord::Migration
def self.up
add_column :users, :activation_code, :string,:limit => 40
add_column :users, :activated_at, :datetime
end
def self.down
remove_column :users, :activated_at
remove_column :users, :activation_code
end
end
rake db:migrate
2.account_controller改成下面这样-->
class AccountController < ApplicationController
# Be sure to include AuthenticationSystem in Application Controller instead
#include AuthenticatedSystem
# If you want "remember me" functionality, add this before_filter to Application Controller
before_filter :login_from_cookie
# say something nice, you goof! something sweet.
def index
redirect_to(:action => 'signup') unless logged_in? || User.count > 0
end
def login
return unless request.post?
self.current_user = User.authenticate(params[:login], params[:password])
if logged_in?
if params[:remember_me] == "1"
self.current_user.remember_me
cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
end
redirect_back_or_default(:controller => '/account', :action => 'index')
flash[:notice] = "Logged in successfully"
else
flash[:notice] = "该账号尚未激活"
note_failed_signin
@login = params[:login]
@remember_me = params[:remember_me]
render :action => 'login'
end
#
end
def signup
@user = User.new(params[:user])
return unless request.post?
success = @user && @user.save
if success && @user.errors.empty?
redirect_back_or_default('/')
flash[:notice] = "很感谢你注册!我们已经发了一封激活邮件到你的注册邮箱里,请注意查收."
else
flash[:error] = "We couldn't set up that account, sorry. Please try again, or contact an admin (link is above)."
render :action => 'signup'
end
#================
#@user.save!
#self.current_user = @user
#redirect_back_or_default(:controller => '/account', :action => 'index')
# flash[:notice] = "Thanks for signing up!"
#rescue ActiveRecord::RecordInvalid
#render :action => 'signup'
end
#邮箱激活================================================================
def activate
# logout_keeping_session!
user = User.find_by_activation_code(params[:activation_code]) unless params[:activation_code].blank?
case
when (!params[:activation_code].blank?) && user && !user.active?
user.activate!
flash[:notice] = "账号已激活! 请登录你的系统."
redirect_to '/account/login'
when params[:activation_code].blank?
flash[:error] = "The activation code was missing. Please follow the URL from your email."
redirect_back_or_default('/')
else
flash[:error] = "We couldn't find a user with that activation code -- check your email? Or maybe you've already activated -- try signing in."
redirect_back_or_default('/')
end
end
#=========================================================================
def logout
self.current_user.forget_me if logged_in?
cookies.delete :auth_token
reset_session
flash[:notice] = "You have been logged out."
redirect_back_or_default(:controller => '/account', :action => 'index')
end
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
end
protected
# Track failed login attempts
def note_failed_signin
flash[:error] = "Couldn't log you in as '#{params[:login]}'"
logger.warn "Failed login for '#{params[:login]}' from #{request.remote_ip} at #{Time.now.utc}"
end
end
3.user.rb里也要作相关修改-->
before_create :make_activation_code
#邮箱激活==========================================================
# Activates the user in the database.
def activate!
@activated = true
self.activated_at = Time.now.utc
self.activation_code = nil
save(false)
end
# Returns true if the user has just been activated.
def recently_activated?
@activated
end
def active?
# the existence of an activation code means they have not activated yet
activation_code.nil?
end
#邮箱激活==========================================================
#邮箱激活=====================================================
def make_activation_code
self.activation_code = self.class.encrypt(Time.now, 10.times.map { rand.to_s })
end
#邮箱激活==========================================================
4.这时候你的user_notifier.rb应该是这样的
class UserNotifier < ActionMailer::Base
@@session = ActionController::Integration::Session.new
def forgot_password(user)
setup_email(user)
@subject += "密码重置"
@body[:url] = @@session.url_for(:controller => "account",
:action => "reset_password",
:id => user.pw_reset_code,
:only_path => false )
end
#邮箱激活==============================================================
def signup_notification(user)
setup_email(user)
@subject += '亲爱的用户,请去邮箱激活你的用户吧.谢谢!'
@body[:url] = "http://#{SITE_URL}/activate/#{user.activation_code}"
end
def activation(user)
setup_email(user)
@subject += 'Your account has been activated!'
@body[:url] = "http://#{SITE_URL}/"
end
#邮箱激活==============================================================
protected
def setup_email(user)
@recipients = "#{user.email}"
@from = "#{ADMINEMAIL}"
@subject = "[#{SITE_URL}] "
@sent_on = Time.now
@body[:user] = user
@headers = {}
end
end
5.user_observer.rb里变成这样了
class UserObserver < ActiveRecord::Observer
def after_create(user)
UserNotifier.deliver_signup_notification(user)
end
def after_save(user)
UserNotifier.deliver_activation(user) if user.recently_activated?
UserNotifier.deliver_forgot_password(user) if user.password_forgotten
end
end
6.在route.rb里加一句
map.activate '/activate/:activation_code', :controller => 'account', :action => 'activate', :activation_code => nil
7.打开enviroment.rb
SITE_URL = "localhost:3000"
ADMINEMAIL = "XXX@163.com"
8.在views/user_notifier增加两个文件,分别为signup_notification.html.erb和activation.html.erb
(1)signup_notification.html.erb
Your account has been created.
Username: <%= @user.login %>
Password: <%= @user.password %>
Visit this url to activate your account:
<%= @url %>
(2)activation.html.erb
<%= @user.login %>, your account has been activated. You may now start adding your plugins:
<%= @url %>
反正暂时是成功的....
分享到:
相关推荐
最后,虽然acts_as_authenticated在当时是一个流行的解决方案,但随着Rails的发展,现在有更多的现代认证解决方案出现,如devise和warden。这些现代库提供了更多功能,如邮件验证、密码重置、多因素认证等,并且社区...
acts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_list.raracts_as_...
在Ruby on Rails框架中,`acts_as_rateable`是一个非常实用的插件,它允许用户对模型进行评分,从而实现简单的打分功能。这个插件是Rails社区中的一个开源项目,旨在简化应用中的评分系统集成。在本篇讨论中,我们将...
acts_as_list, 用于管理列表的ActiveRecord插件 ActsAsList描述这个 acts_as 扩展提供了对列表中的多个对象进行排序和重新排序的功能。 具有指定的类的类需要在映射的数据库表上定义为整数的position 列。 0.8.0升级...
"acts_as_paranoid" 是一个在Ruby on Rails框架中常用的gem,用于处理数据库记录的软删除(soft delete)。在数据库操作中,通常我们会遇到删除数据的需求,但直接删除可能会导致数据丢失,尤其是在生产环境中。...
#ActsAsCategory acts_as_category (Version 2.0 beta)acts_as_category,是acts_as插件在acts_as_tree风格的Ruby on Rails的ActiveRecord的模式,但有一些额外的功能,以及多种便捷视图助手。例子(有关实例方法和...
用户只需在模型中加入acts_as_nested_set,即可享受其提供的各种操作,如添加、移动、删除节点,以及获取子节点和祖先节点等。 3. BetterNestedSet的改进与特性 BetterNestedSet在acts_as_nested_set的基础上做了...
介绍插件,用于对记录进行排序(使用 gem)安装要启用rails_admin_acts_as_list,请将以下内容添加到您的Gemfile : gem 'rails_admin_acts_as_list'gem 'rails_admin' 重要提示: rails_admin_acts_as_list之前必须...
Api-acts_as_api.zip,使在rails中创建api响应变得简单和有趣,一个api可以被认为是多个软件设备之间通信的指导手册。例如,api可用于web应用程序之间的数据库通信。通过提取实现并将数据放弃到对象中,api简化了编程...
acts_as_restful_list 就像acts_as_list 一样,但不必使用非标准方法调用(如insert_at)来弄乱您的代码,acts_as_restful_list 使管理列表变得简单。 您可以像更新其他任何内容一样更新 position 属性,其余的都...
active_record-acts_as, 模拟activerecord模型的多表继承 插件开发已经移动这个项目的积极发展已经转移到了的krautcomputing fork 。 请在那里报告问题并打开 PRs ! ActiveRecord::ActsAs这是对 acts_as_relation的...
已知的问题在同一模型上使用acts_as_paranoid和ActiveStorage 。 您不能直接以已删除状态创建模型,也不能在删除后更新模型。用法安装gem: gem 'acts_as_paranoid' , '~> 0.7.0' bundle install创建迁移bin/rails ...
gem 'acts_as_liked' 然后执行: $ bundle 运行生成器: $ rails generate acts_as_liked 并且不要忘记迁移您的数据库 $ rake db:migrate 用法 可爱的模特 将acts_as_likeable添加到任何模型,它的实例可以被...
与acts_as_commentable 兼容(但需要更改数据库架构) 要求 此 gem 的 2.x 版本仅适用于 Rails 4 及更高版本。 对于此 gem 的 Rails 3.x 兼容版本,请使用版本 1.2.0。 这个宝石取决于 CollectiveIdea 的 Awesome ...
gem 'acts_as_commentable' Rails gem 'acts_as_commentable' , '3.0.1' Rails gem 'acts_as_commentable' , git: 'git@github.com:jackdempsey/acts_as_commentable.git' , branch: '2.x' 生成器 Rails 3+ ...
acts_as_shopping_cart 一个简单的购物车实现。 您可以找到示例应用程序。 安装 滑轨3 从0.2.0版开始,不再支持Rails 3。 如果您仍需要在Rails 3应用程序中实现此gem,请使用0-1-x分支 将其包含在您的Gemfile中 ...
使用acts_as_aliased在模型中启用别名: model Company < ActiveRecord::Base acts_as_aliased end 这假设您的公司模型中有一个名为name的列。 您可以通过传递column参数来指定不同的column : model ...
acts_as_xapian将Xapian的强大功能与Rails的优雅集成,使得开发者能够在Rails应用中轻松实现全文搜索功能。 该插件的主要特性包括: 1. **无缝集成**:acts_as_xapian能够无缝地与Rails模型集成,只需在模型中添加...
这个gem的使用很简单,你只需要在我们想要的带有实时索引的模型中执行acts_as_realtime方法,该方法可以携带一系列参数来稍微修改行为和其他强制性参数,但这些将是后面解释了,另外一步就是在视图中插入必要的代码...
gem 'acts_as_nps_rateable' 如果要将此gem添加到rails 3应用程序,则应将行更改为: gem 'acts_as_nps_rateable', '=0.0.4' 然后执行: $ bundle 或将其自己安装为: $ gem install acts_as_nps_rateable 一旦...