我想向学生和卫报发送电子邮件。我有3个表学生,卫报和用户。学生和监护人也在用户表中。但是,当我发送电子邮件时,它会使用第一个用户的电子邮件地址而不是“卫报”。 这是我的代码:
student.rb
def student_email(subject, recipient, message)
user = User.discover(id, recipient)
UserMailer.student_email(user, subject, message).deliver
end
student_controller.rb
def send_email
subject = params[:subject]
recipient = params[:email][:recipient]
message = params[:message]
if subject.empty? || recipient.empty? || message.empty?
flash[:alert] = t('email_invalid')
@student = Student.shod(params[:student_id])
redirect_to email_students_path(@student)
else
@student = Student.shod(params[:student_id])
@student.student_email(subject, recipient, message)
flash[:notice] = t('sent_email')
redirect_to email_students_path(@student)
end
end
user_mailer.rb
def student_email(user, subject, message)
@user = user
user = "#{@user.email}"
@message = message
mail(to: @user.email, subject: "#{subject}")
end
user.rb
class User < ActiveRecord::Base
def self.current
Thread.current[:user]
end
# setter for current user
def self.current=(user)
Thread.current[:user] = user
end
# Method for find student role and return
def student
return unless role == 'Student'
Student.shod(student_id)
ArchivedStudent.where(student_id: student_id).take
end
def parent
return unless role == 'Parent'
Student.shod(student_id)
Gaurdian.where(student_id: student_id).take
end
# method return if role is employeee
def employee
return unless role == 'Employee'
Employee.shod(employee_id)
end
# create users employee
def create_user_employee(employee_number, email)
employee_number.each do |emp_no|
UserEmployees.create(email: email, employee_number: emp_no)
end
end
def create_user_student(admission_no, email,phone1)
admission_no.each do |admission_no|
UserStudent.create(email: email, admission_no: admission_no,phone1: phone1)
end
end
end