无法在 Rails 中更新动作文本富文本
Posted
技术标签:
【中文标题】无法在 Rails 中更新动作文本富文本【英文标题】:Can't update Action Text Rich Text in Rails 【发布时间】:2020-08-10 10:24:02 【问题描述】:我有一个具有以下属性的模型:
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
任何帮助将不胜感激。谢谢。
【问题讨论】:
【参考方案1】:在我的情况下,这个错误是因为我使用 UUID 值作为标识符,我忘记在迁移 active_storage_tables.active_storage.rb 和 type: :uuid 上添加strong>action_text_tables.action_text.rb
【讨论】:
【参考方案2】:也许您需要在your_model_controller.rb
中将content
列入白名单
点赞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
【讨论】:
抱歉,我忘记更新这个问题了。问题实际上只是将 Rails 设置为 API。 ActionText 仅与 API 无法按预期工作。我通过在属性上调用.to_html
来使其工作,但随后在我的邮件中,还必须在此之后添加 .html_safe
。不理想,但让它工作。 github.com/rails/rails/issues/39266以上是关于无法在 Rails 中更新动作文本富文本的主要内容,如果未能解决你的问题,请参考以下文章