找到你要的答案

Q:Would like Devise Models::Recoverable to send different reset instructions based on user state

Q:想设计模型::根据用户状态可恢复发送不同的复位指令

Our User model contains a parameter called user_state. We would like to send four different email instructions on how and why the user must reset their password based on the user_state: new_user, password_forgotten (user has requested to reset password), inactive (account hasn't been used lately), and suspended (login disabled due to some undesirable behavior).

In all of the above states, we would first like to set the current password to a random password and then send a custom email for each state type.

I have found the Devise send_reset_password_instructions method which sends the reset email. But I can not figure out a reasonable way to send different email content based on the user_state listed above.

Any suggestions?

我们的用户模型包含一个参数称为user_state。我们想把四个不同的电子邮件说明如何以及为什么用户必须重置其密码的基础上user_state:new_user,password_forgotten(用户要求重置密码),无效(账户一直没有使用,最近)和暂停(由于一些不良行为登录禁用)。

在上述所有状态中,我们首先要将当前密码设置为随机密码,然后为每个状态类型发送自定义电子邮件。

我发现设计send_reset_password_instructions方法发送重置邮件。但我不能找出基于上述user_state发送不同的邮件内容的合理途径。

有什么建议吗?

answer1: 回答1:

You can do that by polymorphing the mailer view. Lets assume you have the reset_password_email(user) method in your UserMailer to send that message. And you have 2 possible states: master and slave (just an example).

So then to generate 2 different message bodies you put them in different partials in app/views/user_mailer/_master.html.erb and app/views/user_mailer/_slave.html.erb (or whatever html parser you are using, I prefer slim).

And then you just need to put the correct call in your app/views/user_mailer/reset_password_email.html.erb like this:

<%= render partial: @user.state, user: @user %>

你可以通过晶邮件视图。让我们假设你有reset_password_email(用户)在你的usermailer方法发送消息。你有2种可能的状态:主人和奴隶(只是一个例子)。

从而产生2种不同的消息体,你把它们放在应用程序/观点/ user_mailer / _master.html.erb和APP /意见/ user_mailer / _slave.html.erb不同部分(或任何你使用的HTML解析器,我更喜欢苗条)。

然后你只需要把正确的调用应用程序/观点/ user_mailer / reset_password_email.html.erb这样:

& lt;% =渲染部分:@user.state,用户:@用户% & gt;

ruby-on-rails  ruby-on-rails-4  devise