You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails 6中ActionText显示携带HTML标签问题求助

Action Text 显示HTML标签问题求助

我找了很多地方都没找到解决方案,只见过一个类似问题的帖子。尝试用sanitize方法时出现错误:no implicit conversion of ActionText::RichText into String,现在想先解决HTML标签直接显示的原始问题,当前显示效果如下:

HTML标签直接显示的效果

以下是我已经完成的配置:

  • 在Comment模型中添加了has_rich_text:
class Comment < ApplicationRecord
  belongs_to :user
  belongs_to :post
  validates :body, presence: true, allow_blank: false
  has_rich_text :body
end
  • 在application.js中添加导入:
import "trix"
import "@rails/actiontext"
  • 在actiontext.scss中添加导入:
@import "trix/dist/trix";
  • 在application.css.scss中添加导入:
@import "./actiontext.scss";
  • 在表单中添加了富文本区域:
<%= form_with model: [@post, @comment], local: true do |f| %>
    <%= f.rich_text_area :body, class: 'form-control greyh' %>
    <%= f.submit 'Comment', class: 'btn btn-success' %>
 <% end %>
  • 当前使用的显示代码:
<%="[#{comment.created_at.strftime("%I :%M %p")}] #{comment.user.username}: #{comment.body}" %>

现在我实在不知道还能尝试什么方法,恳请大家帮忙解决。

内容的提问来源于stack exchange,提问作者user2023608

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 15:20:36