Ruby on rails 如何使用rails发送确认电子邮件?
我正在尝试在rails 4.2.0应用程序中设置逻辑,其中用户必须先确认其用户帐户,然后才能登录站点/rails应用程序。基本上,我有一个注册表单,用户可以输入电子邮件/密码和他们的注册信息。在此过程中,将向他们的地址发送一封带有确认令牌的电子邮件,该令牌应为他们提供确认其帐户的链接。我不确定如何使用确认标记,因此它会将数据库中的布尔值从false更改为true。我将发布到目前为止我已经实现的内容 用户\u controller.rb confirmation.text.erbRuby on rails 如何使用rails发送确认电子邮件?,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我正在尝试在rails 4.2.0应用程序中设置逻辑,其中用户必须先确认其用户帐户,然后才能登录站点/rails应用程序。基本上,我有一个注册表单,用户可以输入电子邮件/密码和他们的注册信息。在此过程中,将向他们的地址发送一封带有确认令牌的电子邮件,该令牌应为他们提供确认其帐户的链接。我不确定如何使用确认标记,因此它会将数据库中的布尔值从false更改为true。我将发布到目前为止我已经实现的内容 用户\u controller.rb confirmation.text.erb 最后,我将确认逻
最后,我将确认逻辑分离到它自己的控制器中,即远离users_controller.rb,这允许我将以下行添加到我的routes.rb 这允许我编辑confirmation.text.erb并在电子邮件中添加以下链接
<%= edit_confirmation_url(@user.confirmation_token) %>
因此,当一个人收到确认其帐户的电子邮件时,它将路由到确认控制器的编辑操作,该编辑操作调用更新操作,并确认帐户。控制器如下所示:
确认\u controller.rb
类确认控制器“确认已过期。”
#elseif@user.update_属性(参数[:user])
elsif@user.update_属性(已确认:true)
将\u重定向到root\u url,:notice=>“您的帐户已被确认。”
其他的
渲染:新
终止
终止
终止
为什么不使用Desive?您可以看看他们是如何实现可确认模块的:
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.
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]
resources :confirmations
<%= edit_confirmation_url(@user.confirmation_token) %>
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