设计如何将current_user传递给confirm_instructions邮件程序

vlw*_*lls 5 ruby-on-rails devise

我有Devise设置和woking很棒.我使用确认,并根据他们的2步注册流程指南进行了修改:

确认时设置密码

我最后一个要求是我遇到了麻烦.

我们有两个场景

1)用户可以注册为新的

2)登录用户(current_user)可以创建新用户.当登录用户创建新用户时,我希望能够将他们的电子邮件添加到发送给新创建用户的确认电子邮件中

在电子邮件向我需要在current_user.email通过某种方式,如果用户通过登录的用户创建一个新的注册用户.我会那么做一个简单的,如果检查和添加一些额外的文本电子邮件.

目前,confirmation_instructions.html.erb:

<p>Welcome <%= @resource.email %>!</p>

<p>You can confirm your account email through the link below:</p>

<p><%= link_to 'Confirm account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %></p>
Run Code Online (Sandbox Code Playgroud)

我需要的是类似的东西

<p>Welcome <%= @resource.email %>!</p>

<% if !@user.email.nil? %>
   <p> some additional welcome text here from <%= @user.email %> </p>
<% end %>

<p>You can confirm your account email through the link below:</p>

<p><%= link_to 'Confirm account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %></p>
Run Code Online (Sandbox Code Playgroud)

我一直在与自定义邮件来回奔波,没有任何快乐.有人可以帮助我,我相信这里有一些简单的东西.

对于信息(我知道这是不是最好的方式,但我们是为演示目的拼凑一个非常快速的应用程序)用户创建在上午的电子邮件地址输入一个新的联系人.如果电子邮件地址不存在于用户表存在,则创建了一个新的用户,则接触关系创建(控制器的片段):

class DashboardController < ApplicationController
     before_filter :authenticate_user!

  def show
    @contacts = current_user.contacts
  end

  def createcontact
    user2 = User.find_by_email(params[:contact_email])
    if user2.nil?
            newContact = User.create(:email => params[:contact_email])
            if newContact.save
                 current_user.newUserContact(newContact)
                 redirect_to dashboard_path, :notice => "conact has been saved as well as a new contact"
            else
                redirect_to dashboard_path, :notice => "ERROR saving contact"
            end
    else
      .
      .
      .
      .
Run Code Online (Sandbox Code Playgroud)

Ash*_*aka 4

按照本教程设置自定义邮件程序。

在 config/initializers/devise.rb 中:

config.mailer = "UserMailer".
Run Code Online (Sandbox Code Playgroud)

在文件夹 app/mailers 中创建一个继承自 Devise 邮件程序的新邮件程序:

# user_mailer.rb
class UserMailer < Devise::Mailer

  def invite(sender, recipient)
    @sender = sender
    @recipient = recipient

    mail( :to => recipient.email,
          :subject => "Invite by #{sender.name}"
        )
  end
end
Run Code Online (Sandbox Code Playgroud)

现在,将您的设计邮件程序视图移动到文件夹 app/views/user_mailer。创建一个新的电子邮件视图,您可以在其中使用变量@sender 和@recipient。

# invite.html.erb
<p>Welcome <%= @recipient.email %>!</p>

<% if @sender.email? %>
  <p> some additional welcome text here from <%= @sender.email %> </p>
<% end %>
Run Code Online (Sandbox Code Playgroud)

现在,在您的控制器中,您可以调用以下命令:

UserMailer.invite(current_user, newContact).deliver
Run Code Online (Sandbox Code Playgroud)