我有一个具有以下属性的模型:
has_rich_text :content
但是,我无法将数据保存到其中,也没有错误。我也无法使用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
任何帮助将不胜感激。谢谢。
答案 0 :(得分:0)
也许您需要将content
中的your_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
答案 1 :(得分:0)
就我而言,这个错误是因为我使用 UUID 值作为标识符,而我忘记在迁移 active_storage_tables.active_storage.rb 和 < strong>action_text_tables.action_text.rb