我试过了什么@H_404_3@
我的Users :: InvitesController中的@group Devise :: InvitationsController create方法在电子邮件模板中未定义.@H_404_3@
试图添加:skip_invitation =>在我的创建中是真的,然后手动发送电子邮件…@H_404_3@
self.resource = resource_class.invite!(params[resource_name],current_inviter,:skip_invitation => true) ::Devise.mailer.invitation_instructions(self.resource).deliver
但是这给出了错误的参数数量,所以有一些我从文档中读取不正确的东西.@H_404_3@
唯一的方法似乎是这样,但是如果有更好的方法使用提供的模板和设计邮件程序,我很好奇@H_404_3@
在我的/ app / controller / users / InvitesController#中创建
(继承自InvitationsController)@H_404_3@
self.resource = resource_class.invite!(params[resource_name],current_inviter) do |u| u.skip_invitation = true end UserMailer.invitation_instructions(self.resource,@object).deliver
其中UserMailer是我的一般(标准)动作邮件程序,并且像…@H_404_3@
def invitation_instructions(resource,inviter,object) @resource = resource @object = object @inviter = inviter mail(:to => resource.email,:subject => 'New invitation from ' + inviter.first_name) end
解决方法
首先创建一个从Devise :: Mailer扩展的自定义邮件程序:@H_404_3@
class MyMailer < 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 end
然后,在config / initializers / devise.rb中,将config.mailer设置为“MyMailer”.这将允许您覆盖任何设计发送和自定义您的喜好的电子邮件.@H_404_3@
那么你可以重写invitable_instructions,如下所示:@H_404_3@
def invitation_instructions(record,token,opts={}) # Determine a way to set object -- likely through a query of some type @object = get_object_for(record) opts[:subject] = 'New invitation from ' + inviter.first_name super end
从你的例子的主要观点是传递额外的数据来设置@ group / @ object.要做到这一点,我会亲自在邮件程序中使用某种类型的查询(不干净,但它被封装,因此较少“神奇”)来检索这些对象.@H_404_3@
另外,如果您想使用自定义邮件模板而不是devise,您可以将它们简单地添加到app / views / my_mailer /目录中,并且设计将更喜欢该目录中的电子邮件来自gem的电子邮件.@H_404_3@