如何使用 rails 发送确认邮件?
how to send confirmation email using rails?
我正在尝试在 rails 4.2.0 应用程序中设置逻辑,用户必须先确认他们的用户帐户才能登录网站/rails 应用程序。基本上,我有一个注册表单,一个人可以在其中输入电子邮件/密码并进行注册。在此过程中,会向他们的地址发送一封带有确认令牌的电子邮件,该令牌应为他们提供 link 以确认他们的帐户。我不确定如何使用确认令牌,因此它将数据库中的布尔值从 false 更改为 true。我将 post 到目前为止实现的内容。
users_controller.rb
def create
@user = User.new(user_params)
if @user.save
# send confirmation email after user has been created.
@user.send_confirmation
session[:user_id] = @user.id
redirect_to root_url, notice: "Thank you for signing up!"
else
render "new"
end
end
def confirm
@user = User.find_by_confirmation_token!(params[:id])
if @user.update_attributes(confirmed: true)
redirect_to login_path
end
end
confirmation.text.erb
To confirm your account, click the URL below.
<%= user_url(@user.confirmation_token) %>
<%= url_for(controller: 'users', action: 'confirm') %>
If you did not request your account creation, just ignore this email and your account will not be created.
routes.rb
Rails.application.routes.draw do
resources :articles do
resources :comments
end
get 'resume' => 'resume#index'
get 'signup' => 'users#new'
get 'login' =>'sessions#new'
get 'logout' => 'sessions#destroy'
# the below route led to a rails routing error
# get 'confirm' => 'users/:confirmation_token#confirm'
resources :users
resources :sessions
resources :password_resets
# route to hopefully get confirmation link working :-/
match '/users/:confirmation_token', :to => 'users#confirm', via: [:post, :get]
# test route
match 'users/foo', :to => 'users#foo', via: [:post, :get]
root "articles#index"
# Added below route for correct "resumé" spelling
get 'resumé', :to =>"resume#index"
# get 'about#index'
get 'about' => 'about#index'
get 'contact' => 'contact#contact'
resources :about
resources :contact
match ':controller(/:action(/:id))(.:format)', via: [:post, :get]
我最终将确认逻辑分离到它自己的控制器中,即远离 users_controller.rb 这允许我将以下行添加到我的 routes.rb
resources :confirmations
这让我可以编辑 confirmation.text.erb 并将以下内容 link 放入电子邮件中,
<%= edit_confirmation_url(@user.confirmation_token) %>
因此,当一个人收到一封电子邮件以确认他们的帐户时,它会路由到确认控制器的编辑操作,编辑操作调用更新操作,并确认帐户。控制器如下所示,
confirmations_controller.rb
class ConfirmationsController < ApplicationController
def new
end
def edit
@user = User.find_by_confirmation_token!(params[:id])
update
end
def update
# @user = User.find_by_confirmation_token!(params[:id])
if @user.confirmation_sent_at < 2.hours.ago
redirect_to new_confirmation_path, :alert => "Confirmation has expired."
# elseif @user.update_attributes(params[:user])
elsif @user.update_attributes(confirmed: true)
redirect_to root_url, :notice => "Your account has been confirmed."
else
render :new
end
end
end
我正在尝试在 rails 4.2.0 应用程序中设置逻辑,用户必须先确认他们的用户帐户才能登录网站/rails 应用程序。基本上,我有一个注册表单,一个人可以在其中输入电子邮件/密码并进行注册。在此过程中,会向他们的地址发送一封带有确认令牌的电子邮件,该令牌应为他们提供 link 以确认他们的帐户。我不确定如何使用确认令牌,因此它将数据库中的布尔值从 false 更改为 true。我将 post 到目前为止实现的内容。
users_controller.rb
def create
@user = User.new(user_params)
if @user.save
# send confirmation email after user has been created.
@user.send_confirmation
session[:user_id] = @user.id
redirect_to root_url, notice: "Thank you for signing up!"
else
render "new"
end
end
def confirm
@user = User.find_by_confirmation_token!(params[:id])
if @user.update_attributes(confirmed: true)
redirect_to login_path
end
end
confirmation.text.erb
To confirm your account, click the URL below.
<%= user_url(@user.confirmation_token) %>
<%= url_for(controller: 'users', action: 'confirm') %>
If you did not request your account creation, just ignore this email and your account will not be created.
routes.rb
Rails.application.routes.draw do
resources :articles do
resources :comments
end
get 'resume' => 'resume#index'
get 'signup' => 'users#new'
get 'login' =>'sessions#new'
get 'logout' => 'sessions#destroy'
# the below route led to a rails routing error
# get 'confirm' => 'users/:confirmation_token#confirm'
resources :users
resources :sessions
resources :password_resets
# route to hopefully get confirmation link working :-/
match '/users/:confirmation_token', :to => 'users#confirm', via: [:post, :get]
# test route
match 'users/foo', :to => 'users#foo', via: [:post, :get]
root "articles#index"
# Added below route for correct "resumé" spelling
get 'resumé', :to =>"resume#index"
# get 'about#index'
get 'about' => 'about#index'
get 'contact' => 'contact#contact'
resources :about
resources :contact
match ':controller(/:action(/:id))(.:format)', via: [:post, :get]
我最终将确认逻辑分离到它自己的控制器中,即远离 users_controller.rb 这允许我将以下行添加到我的 routes.rb
resources :confirmations
这让我可以编辑 confirmation.text.erb 并将以下内容 link 放入电子邮件中,
<%= edit_confirmation_url(@user.confirmation_token) %>
因此,当一个人收到一封电子邮件以确认他们的帐户时,它会路由到确认控制器的编辑操作,编辑操作调用更新操作,并确认帐户。控制器如下所示,
confirmations_controller.rb
class ConfirmationsController < ApplicationController
def new
end
def edit
@user = User.find_by_confirmation_token!(params[:id])
update
end
def update
# @user = User.find_by_confirmation_token!(params[:id])
if @user.confirmation_sent_at < 2.hours.ago
redirect_to new_confirmation_path, :alert => "Confirmation has expired."
# elseif @user.update_attributes(params[:user])
elsif @user.update_attributes(confirmed: true)
redirect_to root_url, :notice => "Your account has been confirmed."
else
render :new
end
end
end