首页 > 解决方案 > 是否可以在 rails mailer 的主题中使用实例或变量,以及如何使用:)

问题描述

我想在邮件主题中使用如下实例或变量。所以我想在主题中获取用户名或其他一些用户详细信息,我尝试了几件事,但每次都收到错误。我也找不到有关此的文档。

    mail(:to => "test@gmail, :subject => "current_user.name") or "<%= current_user.name %>"


class PositionMailer < ActionMailer::Base
  default from: "test@gmail.com"
  layout 'mailer'

  def general_message(position, barge_name)

    @position = position
    @barge_name = barge_name


    mail(:to => "1234@gmail.com, :subject => "#{barge_name}")
  end
end

控制器

  def create
    @position = Position.new(position_params)
    if @position.save!
      redirect_to @position
      PositionMailer.general_message(@position, @barge_name).deliver

标签: ruby-on-railsmailersubject

解决方案


要在引号内传递变量,您需要这样做"#{variable.value}

:subject => "#{current_user.name}" 

如果它有权访问,应该这样做current_user

您必须current_user连同位置值一起传递给邮件程序。

因此,无论您从哪里调用 add current_user,都可能看起来像这样。

PositionMailer.general_message(position, current_user).deliver

推荐阅读