superbot
superbot

Reputation: 461

Action Mailer, sending email to user to whom I sent a message

Action Mailer works fine with other models. But I have one tricky model that's evading this. So user can see the profile of another user. On that profile, user can send that other user a message. This message is linked to that other user through this:

Controller:

def create
  @message = current_user.messages.build(message_params)
  @user = User.find(params[:user_id])
  @message.gotten_id = @user.id

While this worked fine on its own, this is proving difficult to use with Action Mailer code:

Controller continued:

if @message.save 
  UserMailer.with(message: @message).new_message.deliver_later

Mailer controller:

def new_message
  @message = params[:message] 
  mail(to: @message.gotten_id.user.email, subject:"You have a new message!")
end

In other models I used, for example, @comment.post.user.email. Which worked well.

The above code of course does not work. I'd appreciate any thoughts.

Upvotes: 0

Views: 187

Answers (1)

Vasilisa
Vasilisa

Reputation: 4640

Since you are using id you need to find the user by it

mail(to: User.find(@message.gotten_id).email, subject:"You have a new message!")

Upvotes: 2

Related Questions