Ruby on rails 无法将收件人的电子邮件传递给rails mailer

Ruby on rails 无法将收件人的电子邮件传递给rails mailer,ruby-on-rails,email,actionmailer,Ruby On Rails,Email,Actionmailer,现在,团队中的任何团队成员都可以向团队发送消息。当他们发送消息时,它只是在团队新闻提要中发布。我正在编写一个故事,向团队发送电子邮件,通知团队已向团队发送消息 我有邮件收发程序,但我无法传递收件人的信息(电子邮件和用户名)。目前,我已经硬编码发送到我的电子邮件 由于原始邮件模型将收件人设置为类团队,因此此信息不可用。此信息仅在类成员上可用。我试过换课,但这打破了其他逻辑 class messages控制器“团队” 有很多:新闻事件,:as=>:主题 验证:发件人,:状态=>true 验证:收件

现在,团队中的任何团队成员都可以向团队发送消息。当他们发送消息时,它只是在团队新闻提要中发布。我正在编写一个故事,向团队发送电子邮件,通知团队已向团队发送消息

我有邮件收发程序,但我无法传递收件人的信息(电子邮件和用户名)。目前,我已经硬编码发送到我的电子邮件

由于原始邮件模型将收件人设置为类团队,因此此信息不可用。此信息仅在类成员上可用。我试过换课,但这打破了其他逻辑


class messages控制器

class消息“成员”
属于:收件人:class\u name=>“团队”
有很多:新闻事件,:as=>:主题
验证:发件人,:状态=>true
验证:收件人,:状态=>true
验证:message,:presence=>true
属性可访问:团队,:团队id,:收件人,:收件人id,:邮件,:图像
创建后{news\u events.create:team=>recipient}
创建后:消息\u电子邮件\u通知
已附加文件:图像,
:style=>{:thumb=>[“200x300>”,:png]},
:default_url=>“/assets/empty.png”,
:path=>“:rails\u env/:class/:attachment/:id\u partition/:style.:extension”
def消息\u电子邮件\u通知
开始
MessageMailer.message\u通知(self.deliver)!
救援=>ex
Rails.logger.error“发送电子邮件时出错”
空气制动
结束
结束
结束

class MessageMailer
我找到了答案并更新了上面的代码

class MessageMailer < ActionMailer::Base
  default from: "Charactr <admin@charactr.co>"

  def message_notification(message)
    @message = message
    team = @message.recipient

    mail(
      bcc: team.members.map(&:email),
      subject: "#{message.sender.username} sent a message to the team."
    )
  end
end
class MessageMailer
您是否考虑过将电子邮件发送到控制器本身而不是AR回拨?我最初是这样做的,但根本无法发送电子邮件。您说的“无法发送电子邮件”是指您收到了错误还是出了什么问题?似乎允许
消息\u sent
接收您需要的参数,并将它们从控制器传入,这将解决您的问题。不确定是什么原因导致您的电子邮件无法发送。可能是您的
development.rb
上的配置?没有错误。它只是没有发送电子邮件。但问题是收件人与团队有关联。{归属于:发件人,:class\u name=>“成员”归属于:收件人,:class\u name=>“团队”}如果收件人有权访问“成员”,我将能够提取收件人的电子邮件。我只是想弄清楚如何从团队中提取个人电子邮件。我弄明白了。我会更新上面的代码
class Message < ActiveRecord::Base
  belongs_to :sender, :class_name => "Member"
  belongs_to :recipient, :class_name => "Team"
  has_many :news_events, :as => :topic

  validates :sender, :presence => true
  validates :recipient, :presence => true
  validates :message, :presence => true

  attr_accessible :team, :team_id, :recipient, :recipient_id, :message, :image

  after_create { news_events.create :team => recipient }
  after_create :message_email_notificitaion

  has_attached_file :image,
    :styles => { :thumb => ["200x300>", :png] },
    :default_url => "/assets/empty.png",
    :path => ":rails_env/:class/:attachment/:id_partition/:style.:extension"

  def message_email_notificitaion
    begin
      MessageMailer.message_notification(self).deliver!
    rescue => ex
      Rails.logger.error "ERROR SENDING EMAIL"
      Airbrake.notify ex
    end
  end
end
 class MessageMailer < ActionMailer::Base
  default from: "Charactr <admin@charactr.co>"

  def message_notification(message)
    @message = message
    team = @message.recipient

    mail(
      bcc: team.members.map(&:email),
      subject: "#{message.sender.username} sent a message to the team."
    )
  end
end
class MessageMailer < ActionMailer::Base
  default from: "Charactr <admin@charactr.co>"

  def message_notification(message)
    @message = message
    team = @message.recipient

    mail(
      bcc: team.members.map(&:email),
      subject: "#{message.sender.username} sent a message to the team."
    )
  end
end