【问题标题】:Rails: Send Email notification to a company when user applies to a jobRails:当用户申请工作时向公司发送电子邮件通知
【发布时间】:2016-04-29 20:16:48
【问题描述】:

我正在一个具有这些关系的工作委员会工作,一个用户可以申请许多工作,并且工作有许多申请者(用户)。一家公司有很多工作。

用户:

has_many :applications

has_many :jobs, :through => :applications, :uniq => true

工作

has_many :applications

has_many :users, :through => :applications

应用

 belongs_to :user 

 belongs_to :job

公司

has_many :jobs, dependent: :destroy

我想实现一个功能,在用户申请工作时向公司发送电子邮件。

这是我的邮件:

class NotificationMailer < ActionMailer::Base
  default from: "email@gmail.com"

  def job_application(company)
    @company = company
    @job = job        
    @applications = @job.applications
    company = @job.application.company
    mail(to: @company.email, subject: 'Ha recibido una postulacion')
  end
end

应用程序.rb

after_create :send_email

def send_email
  NotificationMailer.job_application(self.company).deliver
end

很明显我有一些错误,在用户申请工作后,我必须更改或添加哪些内容才能向公司发送电子邮件?谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby email


    【解决方案1】:
    1. after_create :send_emailsend_email 方法移至 Job 模型,因为您希望在创建 Job 记录时触发此电子邮件(即当用户申请工作时)

    2. send_email 方法的更改(注意争论自我)

      def send_email
        NotificationMailer.job_application(self).deliver
      end
      
    3. 简化您的NotificationMailer 方法

      def job_application(job)
         @applications = job.applications
         mail(to: job.company.email, subject: 'Ha recibido una postulacion')
      end
      

    【讨论】:

    • 谢谢,但在开发过程中,当用户申请工作时,Application.rb 中的代码出现错误“undefined method `company' for #<0xaac7150>
    猜你喜欢
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多