【发布时间】: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 感到困惑。正如您在上面的代码中看到的,如果被邀请访问该站点的用户已经存在于我们的系统中,我使用我自己的邮件程序,我在其中传递了site 和existing_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