【问题标题】:action_text - enable/disable attachments in different fieldsaction_text - 启用/禁用不同字段中的附件
【发布时间】:2020-04-21 11:10:38
【问题描述】:

如何禁用一个字段的 action_text 附件,但启用另一个字段?

我有一个包含 2 个字段的帖子模型 - description_without_attachmentscontent_with_attachments

模型 Post.rb:

  has_rich_text :description_without_attachments
  has_rich_text :content_with_attachments

我发现如果将以下代码添加到packs/application.js 可以阻止所有附件 for trix action_text:

window.addEventListener("trix-file-accept", function(event) {
  event.preventDefault()
  alert("File attachment not supported!")
})

但是我只想阻止某些特定字段。我想它的工作方式如下:

  has_rich_text :description_without_attachments, attachments: false

【问题讨论】:

  • 看起来没有人比我更关心 actiontext/trix...

标签: ruby-on-rails ruby-on-rails-6 trix actiontext


【解决方案1】:

为了在我的项目中执行此操作,我在特定于该字段的编辑器上添加了事件侦听器,而不是 window

在你的情况下,

let editor = document.querySelector('trix-editor#post_description_without_attachments');
editor.addEventListener("trix-file-accept", e => e.preventDefault());

(虽然由于唯一的id 属性而不必要,但我认为在查询选择器中使用trix-editor 元素名称可以更好地记录代码。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-12
    • 2017-10-06
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多