Ruby on Rails - 用户电子邮件验证页面不存在

时间:2017-04-16 16:27:47

标签: ruby-on-rails heroku

我正在尝试通过点击激活电子邮件链接添加注册确认。电子邮件已发送,但是当我单击该链接时,它表示该页面不存在。在heroku上部署。 样品确认链接:

/account_activations/g_dTGV1CbHip7w3w76pqCQ/edit?email=utn49585%40disaq.com

看起来应用程序无法识别此路径。我检查了,激活摘要(g_dTGV1CbHip7w3w76pqCQ)存储在数据库中。我不知道会导致这样的错误。

account_activation.html.erb:

Click the link below in order to activate your account
<%= edit_account_activation_url(@user.activation_token, email: @user.email)%>

account_activation.text.erb:

Click the link below in order to activate your account
<%= edit_account_activation_url(@user.activation_token, email: @user.email)%>

routes.rb中:

Rails.application.routes.draw do
root 'welcome#home'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
end

完整的github: https://github.com/mateusz208345/Ror-My-App

1 个答案:

答案 0 :(得分:0)

this line更改为:

user = User.find_by(email: params[:email])
                ^^^

为什么?

User.find(email: params[:email])实际上意味着:“找到id{:email => "utn49585@disaq.com"}的用户。

我是如何弄明白的?

我在开发模式下启动了应用程序:

bin/rails db:migrate RAILS_ENV=development
bin/rails server

我创建了一个用户,并在日志中看到此电子邮件已呈现:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
      /* Email styles need to be inline */
    </style>
  </head>

  <body>
    Click the link below in order to activate your account https://rails-tutorial-firel.c9users.io/account_activations/nK69XMp3floxtPhV1Jwskg/edit?email=utn49585%40disaq.com
  </body>
</html>

然后我去了:http://localhost:3000/account_activations/nK69XMp3floxtPhV1Jwskg/edit?email=utn49585%40disaq.com

看到了这个错误:

ActiveRecord::RecordNotFound in AccountActivationsController#edit 
Couldn't find User with 'id'={:email=>"utn49585@disaq.com"}

在生产环境中,例如Heroku,ActiveRecord::RecordNotFound errors are treated as 404可能会产生误导。

更改我在答案开头提到的行修复了这个特殊错误。