10

我们有办法禁用操作文本的附件吗?类似下面的 has_rich_text :content, attachment: false

因此,我们可以从 db 中删除 active_storage_blob、active_storage_attachments 表。在这种情况下,只有 action_text_rich_texts 表才能满足目的。

4

2 回答 2

11

绝对地!

  1. 将此添加到您的 application.js 以阻止附件:
    window.addEventListener("trix-file-accept", function(event) {
      event.preventDefault()
      alert("File attachment not supported!")
    })
  1. 隐藏 css 附件按钮 - 在 application.scss 中添加:
    .trix-button-group--file-tools { display: none !important; }

更重要的是,这是在实际应用程序中完成的提交(前 2 个文件):

https://github.com/yshmarov/pikaburuby/commit/77aaa3e072de943470e4bd2c2b3512727c30232d

于 2020-05-18T23:23:58.910 回答
4

我编写了一个可用于后端部分的自定义验证器:

class NoAttachmentsValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    if value.body.attachments.any?
      record.errors[attribute] << I18n.t('errors.messages.attachments_not_allowed')
    end
  end
end

您可以将此代码保存到名为 no_attachments_validator.rb 的文件中,然后在模型中使用它,如下所示:

validates :content, no_attachments: true

这与 Yshmarov 提出的前端修改相结合,效果相当好。

于 2021-01-04T19:52:59.613 回答