简体   繁体   English

Rails 5.1 Mailer错误-模板中的帐户激活参数

[英]Rails 5.1 mailer error - account activation parameter in templates

I'm following Mike Hartl's Rails tutorial & ran into an issue when creating email templates. 我正在关注Mike Hartl的Rails教程,并在创建电子邮件模板时遇到问题。 (Please note the tutorial is based on Rails 5.0.1, while I'm plowing ahead with Rails 5.1.1.) (请注意,本教程基于Rails 5.0.1,而我正在努力使用Rails 5.1.1。)

The tutorial uses :activation_token & :activation_digest attributes to handle the security risks of signup URLs, and a :create_activation_digest hook to set their values before creating the User model. 本教程使用:activation_token:activation_digest属性来处理注册URL的安全风险,并使用:create_activation_digest钩子在创建User模型之前设置其值。

Here's the mailer view template: 这是邮件视图模板:

<%= link_to "Activate", edit_account_activation_url(@user.activation_token, email: @user.email) %>

The issue appears to be telling Mailer to use the value of :activation_token in the :id field of the route. 问题似乎是告诉Mailer在路由的:id字段中使用:activation_token的值。 (A URL definition error) (URL定义错误)

Here's the route definition, from rails routes : 这是路线定义,来自rails routes

edit_account_activation GET    /account_activations/:id/edit(.:format) account_activations#edit

The error feedback: 错误反馈:

No route matches {:action=>"edit", :controller=>"account_activations", :email=>"test7@example.com", :format=>nil, :id=>nil}, possible unmatched constraints: [:id]

I've tried hard-coding id: @user.activation_token in the mailer template, to no avail. 我已经尝试在邮件模板中对ID:@ user.activation_token进行硬编码,但无济于事。

This seems like a easy one, but I'm stumped and a little worried that I'm overlooking something hidden in the transition from Rails 5.0.x to 5.1.x. 这似乎很简单,但是我很困惑,有点担心我忽略了从Rails 5.0.x到5.1.x过渡中隐藏的某些内容。 Any ideas? 有任何想法吗?

You are getting this error because in your config/routes.rb file, you GET /account_activations/:id/edit . 您收到此错误的原因是,在config/routes.rb文件中,您是GET /account_activations/:id/edit That makes it necessary to send an ID which you don't want/need to do. 这使得有必要发送您不需要/不需要的ID。 Now I know that Rails Tutorial uses :id for token. 现在我知道Rails教程使用:id作为令牌。 But you're not defining id: in your link. 但是您没有在链接中定义id: So either you need to change the route, or change the link. 因此,您需要更改路由或更改链接。

<%= link_to "Activate", edit_account_activation_url(id: @user.activation_token, email: @user.email) %>

Notice I define id: . 请注意,我定义了id: Normally outside this tutorial though, you should consider changing your route to GET /account_activations/edit/:token and then look up the user by token. 不过,通常在本教程之外,您应该考虑将路由更改为GET /account_activations/edit/:token ,然后按令牌查找用户。

Thanks all, But the error was related to how I defined the :activation_token hook in the User model. 谢谢,但是错误与我如何在用户模型中定义:activation_token挂钩有关。 All tests now pass. 现在所有测试都通过了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM