【问题标题】:How to pass in extra parameters (org name) to Devise Invitable email如何将额外参数(组织名称)传递到设计邀请电子邮件
【发布时间】:2019-03-19 19:56:54
【问题描述】:

在我的应用程序中,我有用户和网站(可以将其想象为用户和组织)。它们之间有一个名为 SiteUser 的查找表。在站点用户中:

belongs_to :site

belongs_to :user

before_validation :set_user_id, if: ->() { email != nil }

def set_user_id existing_user = User.find_by(email: email) self.user = if existing_user.present? UserMailer.notify_existing_user(site, existing_user).deliver_now unless Rails.env.test? existing_user else User.invite!(email: email) end end

我需要生成的电子邮件的主题包含站点名称。 “Example Company 已邀请您加入他们的网站!”

在电子邮件正文中,我还需要网站标题。

我对如何将站点参数转移到devise invitable 感到困惑。正如您在上面的代码中看到的,如果被邀请访问该站点的用户已经存在于我们的系统中,我使用我自己的邮件程序,我在其中传递了siteexisting_user,所以我可以访问它在我的邮件视图中。

class UserMailer < ApplicationMailer def notify_existing_user(site, user) @site = site @user = user mail to: @user.email, subject: "You've been given access to #{@site.title} in the Dashboard." end end

我不知道如何使用设计邀请邮件程序来类似地完成此操作,当系统中不存在用户时使用该邮件程序。

您能提供的任何帮助将不胜感激!

【问题讨论】:

  • 可能不是您希望的答案,但假设存在某种“当前站点”逻辑,您可以将“current_site_id”存储在您的用户上,然后从您的内部查询它邮件。

标签: ruby-on-rails devise devise-invitable


【解决方案1】:

我实现这一点的方法是在 User 上添加一个名为 invite_site_name 的临时/虚拟属性。因为问题是site 不是用户的属性,所以我无法将site 发送到User.invite!

class User < ActiveRecord::Base
  attr_accessor :invite_site_name

然后在我的CustomDeviseMailer 中我可以访问它:

class CustomDeviseMailer < Devise::Mailer

  helper :application # gives access to all helpers defined within `application_helper`.
  include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
  default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views

  def invitation_instructions(record, token, opts={})
    opts[:subject] = "#{record.invite_site_name} has invited you to their Dashboard! Instructions inside!"

    super
  end
end

【讨论】:

    【解决方案2】:

    你总是可以覆盖设计的 subject_for 方法。在 gems 问题上找到了这个,这也暗示了另一种方式: https://github.com/scambra/devise_invitable/issues/660#issuecomment-277242853

    希望这会有所帮助!

    【讨论】:

    • 我在看这个,这对我来说很有意义。感谢您分享这个。我一直在理解如何将站点 ID 传递到我正在覆盖的方法中(如果我在其中传递侧 ID,我可以进行查找并在邮件程序中获取站点对象)。 User.invite!(email: email, site: site) 导致用户的“未知属性“站点”。因为用户 has_many 站点。但我只想让那个 ID 通过那里,这样我就可以在我的 CustomDeviseMailer 中查询它。
    • 试试self.site,因为您在 SiteUser 内部。或者在方法的开头设置一个变量,比如invitation_site = self.site,然后在你的invite!调用中引用invitation_site
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-13
    • 2023-03-23
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    相关资源
    最近更新 更多