【发布时间】:2015-01-13 00:11:51
【问题描述】:
我正在尝试在 rails 4.2.0 应用程序中设置逻辑,在该应用程序中,人们必须先确认其用户帐户,然后才能登录站点/rails 应用程序。基本上,我有一个注册表单,一个人可以在其中输入电子邮件/密码并进行注册。在此过程中,一封带有确认令牌的电子邮件会发送到他们的地址,该确认令牌应为他们提供一个链接以确认其帐户。我不确定如何使用确认令牌,因此它将数据库中的布尔值从 false 更改为 true。我将发布到目前为止我已经实现的内容。
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]
【问题讨论】:
-
为什么不使用 devise?你可以看看他们是如何实现可确认模块的:rubydoc.info/github/plataformatec/devise/master/Devise/Models/…
标签: ruby-on-rails ruby ruby-on-rails-4