1

我有以下型号,

class Exam 
  has_many :registrations
end

class Registration
  belongs_to: user
  belongs_to: lesson
end

class Lesson
  has_many :exam_registrations
  belongs_to :user
end

class User
  has_many :registrations, :through => lessons
end

我正在尝试设置一个自动电子邮件,每当更新考试时,都会向每个注册的用户发送一封电子邮件。我已经设置了 action mailer 并为其他非嵌套资源工作,但在过去的几天里,这个特殊情况让我感到难过。

邮件程序.rb

  def inform_exam_venue(user, student, lesson, exam_registration, exam)
    @user = user
    @student = student
    @lesson = lesson
    @exam_registration = exam_registration
    @exam = exam
    mail(:to => "#{student.name} <#{student.email}>", :subject => "Exam registration update")
  end

考试控制器.rb

  def update
    @exam = Exam.find(params[:id])

    @exam_registration = @exam.exam_registrations.find(params[:id])

    @lesson = @exam_registration.lesson
    @user = User.where("id =? ", @exam_registration.user_id).first
    @student = Student.where("id =? ", @exam_registration.student_id).first

    respond_to do |format|
      if @exam.update_attributes(params[:exam])
        Mailer.inform_exam_update(@user, @student, @lesson, @exam_registration, @exam).deliver
        format.html { redirect_to @exam, notice: 'Exam was successfully updated.' }
      else
        format.html { render action: "edit" }
      end
    end
  end

我的控制器代码显然是错误的,但我似乎找不到任何关于如何设置它的说明。请帮忙。

4

1 回答 1

1

我在您的代码中发现的唯一错误是您在控制器中使用

Mailer.inform_exam_update

代替

Mailer.inform_exam_venue

因此,您需要将_update更改为_venue

于 2013-09-08T08:25:06.547 回答