【问题标题】:Action text with email带有电子邮件的操作文本
【发布时间】:2020-10-09 07:09:56
【问题描述】:

如何在操作文本中发送邮件正文?

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

我希望 body 是一个字符串,但我在一个像

的对象中得到了 action_text 正文

#<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">

使用 rails 6.0、Ruby 2.7.1

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-6 actiontext


    【解决方案1】:

    ActionText 有几个方法可以从富文本转换:

    试试.to_s()

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

    .to_plain_text()

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

    to_html()

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    相关资源
    最近更新 更多