Actionmailer提供给嵌套资源

时间:2013-09-08 07:05:12

标签: ruby-on-rails controller actionmailer mailer

我有以下型号,

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

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

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

exams_controller.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

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

1 个答案:

答案 0 :(得分:1)

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

Mailer.inform_exam_update

而不是

Mailer.inform_exam_venue

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