3

我有一个具有以下属性的模型:

has_rich_text :content

但是,我无法将数据保存到其中并且没有错误。我也无法使用内容属性创建新记录。

在控制台中:

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

然后,当我尝试创建新记录时,我得到了这个:

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.

来自 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

任何帮助将不胜感激。谢谢你。

4

2 回答 2

3

就我而言,这个错误是因为我使用 UUID 值作为标识符,我忘记在迁移active_storage_tables.active_storage.rbaction_text_tables.action_text.rb上添加type: :uuid

于 2021-08-09T00:56:39.817 回答
1

也许你需要在白名单contentyour_model_controller.rb

喜欢params.require(:comment).permit(:content)

我还建议安装与 action_text 一起使用的 active_storage。

文档写得很好:

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

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

于 2020-05-18T23:37:24.403 回答