【发布时间】:2016-08-10 03:02:52
【问题描述】:
在开始解释问题之前,我将简要解释一下该功能。当用户注册时,他们会在其电子邮件中获得一个激活令牌,该令牌将在 2 小时后过期。我试图实现一个功能,允许用户在他们的电子邮件中重新发送激活令牌。
重新发送激活令牌功能的代码如下所示。
用户控制器中的控制器代码
def resend_verification_email
@user = User.find_by(email: params[:resend_verification_email] [:email].downcase)
if valid_email(params[:resend_verification_email] [:email])
if !@user
redirect_to resend_verification_path
flash[:danger] = "Email does not exist"
elsif
!@user.activated?
UserMailer.resend_activation(@user).deliver_now
flash[:success] = "Check your email for the activation token"
redirect_to resend_verification_path
else
redirect_to resend_verification_path
flash[:success] = "User is already activated."
end
else
flash[:danger] = "Email format is Invalid"
redirect_to resend_verification_path
end
end
帐户激活控制器
class AccountActivationsController < ApplicationController
def edit
user = User.find_by(email: params[:email])
if user && !user.activated? && user.authenticated?(:activation, params[:id]) #the token is actually available by params id
user.activate
log_in user
flash[:success] = "Account activated."
redirect_to home_path
else
flash[:danger] = "Invalid activation link"
redirect_to root_url
end
end
end
邮件控制器方法
def resend_activation(user)
@user = user
@userid = user.id
mail to: user.email, subject: "Account activation"
end
邮件视图
<%= link_to "Activate your account", edit_account_activation_url(id: @user.activation_token, email: @user.email) %>
我收到以下错误。 No route matches {:action=>"edit", :controller=>"account_activations", :email=>"example@gmail.com", :id=>nil} missing required keys: [:id]。
我明白错误在说什么。 url 的第一个参数是令牌,获取方式是通过 params[:id] 因为在 RESTful 路由中,id 始终是第一个参数。如果我去掉 URL,电子邮件就会很好地发送出去。 URL 引发了该错误,我不确定为什么。任何帮助,将不胜感激。谢谢!
我只包括了相关路线。
get 'resend_verification' => 'users#resend_verification'
post 'resend_verification_email' => 'users#resend_verification_email'
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
相关的rake路线
edit_account_activation GET /account_activations/:id/edit(.:format) account_activations#edit
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
resend_verification GET /resend_verification(.:format) users#resend_verification
resend_verification_email POST /resend_verification_email(.:format) users#resend_verification_email
【问题讨论】:
-
我有一个解决办法。但在分享之前,请发布
rake routes的输出。我需要确定我是否正确。 -
@ArunKumar,我在原始问题中包含了 rake 路线。
-
请发布
rake routes的输出。不是您在 routes.rb 中定义的路由 -
@ArunKumar,我包括了用户模型、帐户激活以及 resend_token 路由的 GET 和 POST 请求。
-
你解决过这个问题吗?我也有同样的问题。
标签: ruby-on-rails ruby