3

I have a model with the following attribute:

has_rich_text :content

However, I can't save data to it and there are no errors. I also can't create new records with a content attribute.

In console:

irb(main):011:0> a.content
=> #<ActionText::RichText id: 6, name: "content", body: #<ActionText::Content " ">, record_type: "Section", record_id: 78730, created_at: "2020-04-26 22:32:27", updated_at: "2020-04-26 22:38:30">

irb(main):012:0> a.content = '<p>Hello World</p>'
irb(main):013:0> a.save
   (0.2ms)  BEGIN
  Chapter Load (0.8ms)  SELECT "chapters".* FROM "chapters" WHERE "chapters"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ActiveStorage::Attachment Load (0.4ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3  [["record_id", 6], ["record_type", "ActionText::RichText"], ["name", "embeds"]]
   (0.2ms)  COMMIT
=> true

irb(main):014:0> a.content
=> #<ActionText::RichText id: 6, name: "content", body: #<ActionText::Content " ">, record_type: "Section", record_id: 78730, created_at: "2020-04-26 22:32:27", updated_at: "2020-04-26 22:38:30">

irb(main):015:0> a.content.body
=> #<ActionText::Content " ">

irb(main):016:0> a.reload
  Section Load (0.7ms)  SELECT "sections".* FROM "sections" WHERE "sections"."id" = $1 LIMIT $2  [["id", "78730d87-8dfb-490a-a868-e96dc077b23e"], ["LIMIT", 1]]
=> #<Section id: "78730d87-8dfb-490a-a868-e96dc077b23e", title: "Introduction", position: 1, chapter_id: 1, created_at: "2020-04-26 22:29:57", updated_at: "2020-04-26 22:38:30">

irb(main):017:0> a.content
  ActionText::RichText Load (1.2ms)  SELECT "action_text_rich_texts".* FROM "action_text_rich_texts" WHERE "action_text_rich_texts"."record_id" = $1 AND "action_text_rich_texts"."record_type" = $2 AND "action_text_rich_texts"."name" = $3 LIMIT $4  [["record_id", 78730], ["record_type", "Section"], ["name", "content"], ["LIMIT", 1]]
=> #<ActionText::RichText id: 6, name: "content", body: #<ActionText::Content " ">, record_type: "Section", record_id: 78730, created_at: "2020-04-26 22:32:27", updated_at: "2020-04-26 22:38:30">

Then, when I try to create a new record, I get this:

ActiveRecord::RecordNotUnique (PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_action_text_rich_texts_uniqueness")
DETAIL:  Key (record_type, record_id, name)=(Section, 0, content) already exists.

Action Text from schema.rb

  create_table "action_text_rich_texts", force: :cascade do |t|
    t.string "name", null: false
    t.text "body"
    t.string "record_type", null: false
    t.bigint "record_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["record_type", "record_id", "name"], name: "index_action_text_rich_texts_uniqueness", unique: true
  end

Any help would be greatly appreciated. Thank you.

navi
  • 193
  • 2
  • 12

2 Answers2

3

In my case, this bug has because I'm use UUID values for identifiers and I forget add type: :uuid on migrations active_storage_tables.active_storage.rb and action_text_tables.action_text.rb

1

Maybe you need to whitelist content in the your_model_controller.rb

Like params.require(:comment).permit(:content)

As well I would advise to install active_storage that goes hand in hand with action_text.

The docs are very well written:

https://edgeguides.rubyonrails.org/active_storage_overview.html

https://edgeguides.rubyonrails.org/action_text_overview.html

Yshmarov
  • 3,450
  • 1
  • 22
  • 41
  • Sorry, I forgot to update this question. The issue was actually with setting up rails as API only. ActionText doesn't work as intended with API only. I got it to work by calling `.to_html` on the attribute, but then in my mailers, also had to add `.html_safe` after that. Not ideal, but got it working. https://github.com/rails/rails/issues/39266 – navi May 20 '20 at 00:07