Get plain text of rich text attribute in Rails
23 April 2022 (Updated 23 April 2022)
Suppose you have a Note
model with a rich text attribute named content
. If content
has the value Hello there
, and you run:
Note.content
you’ll get a ActionText::RichText
instance:
#<ActionText::RichText:0x00000001090fdf70
id: 7,
name: "content",
body: #<ActionText::Content "<div class=\"trix-conte...">,
record_type: "Note",
record_id: 7,
created_at: Sat, 23 Apr 2022 14:52:03.372868000 UTC +00:00,
updated_at: Sat, 23 Apr 2022 14:52:03.372868000 UTC +00:00>
irb(main):007:0>
To get the plain text version, you can use:
Note.content.to_plain_text
which will return:
"Hello there"
Tagged:
Rails
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment