I have a rails mailer that currently sends an email successfully anytime a user replies to another user's comment. (the email goes to the person who was replied to). I'm trying to add some dynamic content in the email body like the username of the person who replied and that user's comment itself. I'm not sure how to grab that particular comment within the new_reply.html.erb view so that it shows correctly in my email that is being sent...my code is as follows:
views/comment_mailer/new_reply.html.erb (email content)
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<table width="100%">
<h2 style="color: #428bca">You have a new reply.</h2>
<p>Someone replied with the following comment...</p>
<p><%= comment.body %></p>
<p>To reply back, login to the app...</p>
</table>
</body>
</html>
views/comments/_comment.html.erb (the actual comment view in the app)
<div class="well">
<p class="text-muted">Added on
<%= l(comment.created_at, format: '%B, %d %Y %H:%M:%S') %></p>
<blockquote>
<p><%= comment.body %></p>
<p><%= link_to 'reply', new_comment_path(comment.id) %></p>
</blockquote>
</div>
mailers/comment_mailer.rb (my comment mailer)
class CommentMailer < ApplicationMailer
default from: "notifications@app.com"
def new_reply(parent_comment)
owner = parent_comment.owner
mail(to: owner.email, subject: 'New reply to one of your comments')
end
end
controllers/model_comments_controller.rb (controller for these comments)
def create
@taskrelationship = commentable_type.constantize.find(commentable_id)
@project = @taskrelationship.taskproject
@new_comment = Comment.build_from(@taskrelationship, current_user.id, body)
if @new_comment.save
# create the notification
(@taskrelationship.taskproject.followers.uniq - [current_user]).each do |user|
Notification.create(recipient: user, actor: current_user, action: "posted", notifiable: @new_comment)
end
make_child_comment
end
render 'projects/show_project_task_comments', layout: false
end
private
def make_child_comment
return if comment_id.blank?
parent_comment = Comment.find comment_id
@new_comment.move_to_child_of(parent_comment)
CommentMailer.new_reply(parent_comment).deliver
end
In the new_reply
method in the mailer declare an instance variable @comment that references the comment you'll like to use.
Then in the template, just use @comment to reference it.