Ruby on rails Rails 6-动作文本和;美国石油学会

Ruby on rails Rails 6-动作文本和;美国石油学会,ruby-on-rails,ruby-on-rails-6,actiontext,Ruby On Rails,Ruby On Rails 6,Actiontext,我正在创建一个Rails 6.0.0应用程序,它包含以下模块: 网络-表单/积垢 原料药 我用的地方有个积垢。表单工作正常,我成功地实现了操作文本工件 接下来,我创建了一个JSON API,它公开了通过表单创建的资源。输出JSON如下所示: { "id": 3, "title": "Happy title", "content": { "id": 1, "name": "content", "body": "<di

我正在创建一个Rails 6.0.0应用程序,它包含以下模块:

  • 网络-表单/积垢
  • 原料药
我用的地方有个积垢。表单工作正常,我成功地实现了操作文本工件

接下来,我创建了一个JSON API,它公开了通过表单创建的资源。输出JSON如下所示:

{
    "id": 3,
    "title": "Happy title",
    "content": {
        "id": 1,
        "name": "content",
        "body": "<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas eget tortor venenatis elit laoreet sagittis. In hac habitasse platea dictumst. Mauris nec nisl dapibus sem volutpat ultricies vel eu erat. Nunc at aliquet diam, vel pretium velit. <br><br><action-text-attachment sgid=\"BAh7CEkiCGdpZAY6BkVUSSI0Z2lkOi8vZXhpbi13ZWIvQWN0aXZlU3RvcmFnZTo6QmxvYi81P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==--8f909146395a3dede0d4d5e47ad54d82d9e06367\" content-type=\"image/png\" url=\"http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--6a79e95fd7a0fc0883190bd1201d4f5ecb1f8445/qrcode%20(2).png\" filename=\"qrcode (2).png\" filesize=\"497\" width=\"400\" height=\"400\" presentation=\"gallery\"></action-text-attachment><br><br><strong>Phasellus</strong> at dictum ligula. Sed tempus odio sed elit ultricies, in pulvinar purus dictum. Suspendisse sit amet fermentum lacus. Suspendisse sed tristique lectus.</div>",
        "record_type": "Announcement",
        "record_id": 3,
        "created_at": "2019-10-30T23:25:24.080Z",
        "updated_at": "2019-10-30T23:25:24.138Z"
    },
    "created_at": "2019-10-30T23:25:24.039Z",
    "updated_at": "2019-10-30T23:25:24.139Z"
}
{
“id”:3,
“头衔”:“快乐的头衔”,
“内容”:{
“id”:1,
“名称”:“内容”,
“正文”:"Lorem ipsum dolor sit amet,继续为精英服务。Maecenas eget tortor venenatis elit laoreet sagittis。在居住平台上的言论。Mauris nec Niss dapibus sem在阿利克特直径、维利特维利特维利特维利特维利特维利特维利特维利特维利特维利特维利特维利特维利特维利特维利特维利特维利特维特维尼特维尼特维尼特维尼特维尼特维尼特维尼特维尼特维尼特维尼特维尼特维尼特维尼特维尼特维尼特维尼特维尼特维尼特维尼特维这句话的意思是:“酒后甜酒是一种发酵剂,酒后甜酒是一种酒后甜酒。”,
“记录类型”:“公告”,
“记录id”:3,
“创建时间”:“2019-10-30T23:25:24.080Z”,
“更新时间”:“2019-10-30T23:25:24.138Z”
},
“创建时间”:“2019-10-30T23:25:24.039Z”,
“更新时间”:“2019-10-30T23:25:24.139Z”
}
问题:

如果看到,content.body属性具有Rails操作文本标记:

<action-text-attachment sgid...>

在一天结束时的表单中,Rails操作文本将此标记转换为如下所示的figure HTML标记,以便呈现图像:

<figure class="attachment attachment--preview attachment--png">
    <img src="http://localhost:3000/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--6a79e95fd7a0fc0883190bd1201d4f5ecb1f8445/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCam9VY21WemFYcGxYM1J2WDJ4cGJXbDBXd2RwQWdBRWFRSUFBdz09IiwiZXhwIjpudWxsLCJwdXIiOiJ2YXJpYXRpb24ifX0=--defcd74f258f74bba5b028de265f7a5858cffd0b/qrcode%20(2).png">

  <figcaption class="attachment__caption">
      <span class="attachment__name">qrcode (2).png</span>
      <!--<span class="attachment__size"></span>-->
  </figcaption>
</figure>

qrcode(2).png
问题:

如何使用JSON API呈现相同的HTML


我需要公开此内容,以便在多个客户端应用程序中呈现它。

假设我有
丰富的文本:description
。为了获得纯
body
,我需要用
description.body.调用它,因为它给了我:

"<div class=\"trix-content\">\n  <div>test</div>\n</div>\n"

因此,在保存动作文本的原始输出之前,我创建了一个虚拟列来填充模型的

has_rich_text :rich_body

before_save :update_old_body

private

def update_old_body
  self.body = rich_body&.body&.to_s unless body
end
然后我将
body
放入序列化程序中以生成JSON

但是,如果要迁移较旧的记录,则必须手动创建操作文本记录,否则会出现非常持久的
未定义方法“render\u Action\u Text\u content”
错误。在我的情况下,将回调更改为该方法可以:

def update_old_body
  self.body = rich_body&.body&.to_s unless body
rescue ActionView::Template::Error
  build_rich_text_rich_body
end
完成了

更新

如果您有附件,则需要将其更改为:

self.body = rich_body&.body&.to_rendered_html_with_layout

拯救自己一个受伤的世界。

这不起作用,因为调用Action Text会在
.body
上寻找其他方法,比如
.render\u Action\u Text\u content
。我在回答中以一种非常粗糙的方式解决了这个问题。@okay56k太好了!你的答案在哪里:即使是粗糙的,人们(嗯:我自己)我很想看看。如果你能邮寄,请do@BKSpurgeon已解决并发布:)没错,rich_text_field.body.to_s为您提供纯html内容。
self.body = rich_body&.body&.to_rendered_html_with_layout