7

我创建了一个自定义设计邮件程序来更改设计电子邮件模板在视图中的位置。我做了以下更改:

#/config/initializers/devise
config.mailer = 'CustomDeviseMailer'

# app/mailers/customer_devise_mailer.rb
    def headers_for(action, opts)
        headers = {
            :subject       => translate(devise_mapping, action),
            :from          => mailer_sender(devise_mapping),
            :to            => resource.email,
            :template_path => '/mailers/devise'
        }.merge(opts)
    end

现在我的电子邮件模板位于:/app/views/mailers/devise/

问题是当一个设计邀请.invite!拨打电话时,电子邮件的主题行指出错误:“ translation missing: en.#<Devise::Mapping:0x007fe8fb6f4578>”。

我怀疑我需要对/config/locales/devise_invitable.en.yml文件进行调整。我还用/app/controllers/invitations_controller.rb.

我应该对devise_invitable.en.yml文件进行哪些补充?谢谢。

4

1 回答 1

8

我实施的解决方案是禁用默认的 Devise Invitable 邮件程序,而是使用我自己的邮件程序。该解决方案类似于 Devise Invitable wiki 上的“允许用户创建自定义邀请消息”指南中的解决方案。

我做了以下更改。

将配置更改为自定义邮件:

# config/initializers/devise
config.mailer = 'CustomDeviseMailer'

在您的自定义邮件程序中指定新的设计电子邮件模板路径(并将设计电子邮件模板移动到此文件夹):

# app/mailers/customer_devise_mailer.rb
def headers_for(action, opts)
    super.merge!({template_path: '/mailers/devise'}) # this moves the Devise template path from /views/devise/mailer to /views/mailer/devise
end

使用命令生成一个邮件程序来处理被覆盖的设计邀请电子邮件rails generate mailer InvitableMailer

覆盖 Devise Invitable 控制器上的创建操作。您需要的代码将类似于以下内容。我省略了 respond_to 块,因为它是为我的应用程序定制的。

# controllers/invitations_controller.rb
class InvitationsController < Devise::InvitationsController

    # POST /resource/invitation
    def create
        @invited_user = User.invite!(invite_params, current_inviter) do |u|
            # Skip sending the default Devise Invitable e-mail
            u.skip_invitation = true
        end

        # Set the value for :invitation_sent_at because we skip calling the Devise Invitable method deliver_invitation which normally sets this value
        @invited_user.update_attribute :invitation_sent_at, Time.now.utc unless @invited_user.invitation_sent_at
        # Use our own mailer to send the invitation e-mail
        InvitableMailer.invite_email(@invited_user, current_user).deliver

        respond_to do |format|
            # your own logic here. See the default code in the Devise Invitable controller.
        end
    end
end

邀请控制器现在调用我们生成的邮件程序而不是默认邮件程序。在我们的邮件程序上添加一个发送电子邮件的方法。

# app/mailers/invitable_mailer.rb
class InvitableMailer < ActionMailer::Base
    default from: "blah@blah.com"

    def invite_email(invited_user, current_invitor)
        @invited_user = invited_user
    @current_invitor = current_invitor

        # NOTE: In newever versions of Devise the token variable is :raw_invitation_token instead of :invitation_token
        # I am using Devise 3.0.1
        @token = @invited_user.invitation_token
        @invitation_link = accept_user_invitation_url(:invitation_token => @token)

        mail(to: @invited_user.email,
            from: "blah@blah.com",
            subject: "Invitation to SERVICE",
            template_path: "/mailers/devise")
    end
end

我的自定义邀请电子邮件的模板是app/views/mailers/devise/invite_email.html.erb. 在该电子邮件中,我链接到带有邀请令牌的接受邀请 URL,其中包含以下代码<%= link_to 'Accept invitation', @invitation_link %>

另外,我添加attr_accessible :invitation_sent_at到我的用户模型中,以便我可以:invitation_sent_at attribute从邀请控制器更新。

我希望这有帮助。

于 2014-03-17T23:14:23.703 回答