2

How to send mail body in action text?

Post.rb

has_rich_text :body

after_create :send_email_to_current_user

def send_email_to_current_user
    PostEmailer.create_post(current_user, self)
end

PostEmailer.rb

def create_post(user, post)
   email = user.email
   body = post.body
   mail(to: email, subject: 'create post', body: body)
end

I expected body is a string but I got action_text body in an object like

#<ActionText::RichText id: 12, name: "body", body: #<ActionText::Content "<div class=\"trix-conte...">, record_type: "Post", record_id: 16, created_at: "2020-06-18 11:49:04", updated_at: "2020-06-18 11:49:04">

Using rails 6.0, Ruby 2.7.1

1 Answers1

3

ActionText has a few methods to convert from rich text:

Try .to_s()

mail(to: email, subject: 'create post', body: body.to_s)

or .to_plain_text()

mail(to: email, subject: 'create post', body: body.to_plain_text)

or to_html()

mail(to: email, subject: 'create post', body: body.to_html)
zoilism
  • 86
  • 1
  • 4