-1

I would like to use an instance or variable like below in the mailer subject. So I would like to get the user name or some other user detail in the subject, I tried several thing but I receive an error every time. I can't find documentation about this either.

    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

controller

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

1 Answers1

1

To pass a variable inside quotes you need to do it like this "#{variable.value}

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

should do it if it has access to current_user

You will have to pass current_user to the mailer along with the position value.

So wherever you are calling that from add current_user, probably looks something like this.

PositionMailer.general_message(position, current_user).deliver
Rockwell Rice
  • 3,376
  • 5
  • 33
  • 61
  • oke i added my mailer, I thought current user could be accessed in any view/controller, or should i define it first in the mailer controller? – skosman Nov 01 '19 at 18:32
  • You will have to pass it to the mailer – Rockwell Rice Nov 01 '19 at 18:34
  • so it should be something like: (i added the post) – skosman Nov 01 '19 at 18:46
  • so now when i submit, the subject is empty it says (no subject) I changed the code again – skosman Nov 01 '19 at 19:15
  • 1
    where is the `@barge_name` variable being set in your code? I can't see anything in any of those methods that sets it. If you need to test just fake it first, put `@barge_name = "Test Name"` in the create method and see if it is in the title, and then you can figure out how to set it in the create method dynamically – Rockwell Rice Nov 01 '19 at 20:13
  • 1
    Thank a lot! its working now, by replacing test name by current_user.barge_name. Thanks again for your help, i've learned a lot :) – skosman Nov 01 '19 at 20:47