我正在建立一个网站,如果这两个网站匹配,interviewer
可以向employee
和candidate
发送电子邮件。
在http://localhost:3000/positions/1/candidatures中
我可以访问所有三个电子邮件(interviewer
,employee
和candidate
的电子邮件。
我希望interviewer
单击一个按钮并发送自动电子邮件,该电子邮件将发送到candidate
和employee
。
我不确定该怎么做。
我见过的所有建议都建议创建一个application_mailer
并在邮件程序控制器中写类似的内容:
class MyMailer < ApplicationMailer
def welcome_email(user)
@user = user
mail(to: @user.email, subject: 'Welcome to My Awesome Site')
end
end
先前的代码会将电子邮件发送到@user
。我希望将电子邮件发送到candidate
和interviewer
。
此外,如果我在基本目录中创建控制器,它将永远无法访问candidate
和interviewer
电子邮件。
只能在http://localhost:3000/positions/1/candidatures中检索这两封电子邮件。
(嵌套路线)
我有点困惑,想知道更多建议
其他信息:interviewr
和candidate
来自Devise。 employee
是一个简单的Model.rb
答案 0 :(得分:1)
如果MyMailer
具有方法:
def candidate_email(interviewer, employee, candidate)
@interviewer = interviewer
@employee = employee
@candidate = candidate
mail(to: @candidate.email, subject: 'candidate subject')
end
def employee_email(interviewer, employee, candidate)
@interviewer = interviewer
@employee = employee
@candidate = candidate
mail(to: @employee.email, subject: 'employee subject')
end
您可以在positions
下嵌套另一个控制器,例如:
resources :positions do
resources :candidatures …
resources :candidature_notifications, only: :create
end
然后在控制器操作中可以执行以下操作:
class CandidatureNotificationsController < ApplicationController
def create
# this is probably the similar to what you are loading in Candidatures#index
@position = Position.find(params[:position_id]
@interviewer = …
@employee = …
@candidate = …
MyMailer.candidate_email(@interviewer, @employee, @candidate).deliver
MyMailer.employee_email(@interviewer, @employee, @candidate).deliver
# other notifications here
redirect_to position_candidatures_path(@position), notice: 'Sent notifications'
end
end
您在/positions/1/candidatures
页上的链接将为<%= link_to 'Send emails', positions_candidature_emails(@position), method: :post %>