Nic*_*vre 3 ruby-on-rails trix stimulusjs actiontext
如何使我在 Trix (ActionText) 编辑器中添加的链接在新窗口中使用 target="_blank" 打开链接?
class Post < ApplicationRecord
  has_rich_text :rich_text_content
end
添加刺激控制器richtext_controller.js:
import { Controller } from "stimulus"
export default class extends Controller {
  connect() {
    this.element.querySelectorAll('a').forEach(function(link) {
      if (link.host !== window.location.host) {
        link.target = "_blank"
      }
    })
  }
}
像这样添加您的内容:
<div data-controller="richtext">
  <%= post.rich_text_content %>
</div>