Rails 6 - 动作文本 &应用程序接口 [英] Rails 6 - Action Text & API
问题描述
我正在创建一个 Rails 6.0.0 应用程序,它具有以下模块:
- 网络 - 表单/Cruds
- API
有一个 CRUD,我使用 Action Text.表单工作正常,我成功实现了 Action Text 工件.
接下来,我创建了一个 JSON API,用于公开通过表单创建的资源.输出 JSON 如下所示:
<代码>{身份证":3,"title": "快乐标题",内容": {身份证":1,"name": "内容","body": "<div>Lorem ipsum dolor sat amet, consectetur adipiscing elit.Maecenas eget tortor venenatis elit laoreet sagittis.在 hac 栖息地高原的格言中.Mauris nec nisl dapibus sem volutpat ultricies.的Pretium velit<峰; br><峰; br><作用文本附着SGID = \ BAh7CEkiCGdpZAY6BkVUSSI0Z2lkOi8vZXhpbi13ZWIvQWN0aXZlU3RvcmFnZTo6QmxvYi81P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA == - 8f909146395a3dede0d4d5e47ad54d82d9e06367 \" 内容类型= \ 图像/PNG \" URL = \HTTP://本地主机:3000/轨道/active_storage/斑点/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--6a79e95fd7a0fc0883190bd1201d4f5ecb1f8445/QRCode的%20(2)png格式\"文件名= \ QRCode的(2)png格式\" 文件大小= \ 497 \" 宽度= \ 400 \"height=\"400\"presentation=\"gallery\"></action-text-attachment><br><br><strong>Phasellus</strong> at dictum ligula. Sed tempus odio sed精英 ultricies,在 pu利维纳尔原则.Suspendisse 坐在 amet 发酵 lacus.暂停 sed tristique lectus.</div>","record_type": "公告",记录 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"}
问题:
如果你看到, content.body 属性有 Rails Action Text 标签:
在一天结束时的表单中,Rails Action Text 将此标记转换为像这样的图形 HTML 标记以呈现图像:
问题:
如何使用 JSON API 呈现相同的 HTML?
我需要公开此内容以便在多个客户端应用程序中呈现它.
假设我有 has_rich_text :description
.为了获得纯 body
,我需要使用 description.body.to_s
调用它,因为它给了我:
"<div class=\"trix-content\">\n <div>test</div>\n</div>\n"
无论如何简单的 description.body
会给我:
#I'm creating a Rails 6.0.0 App and it has the following modules:
- Web - Forms / Cruds
- Api
There's a CRUD where I use Action Text. The form is working OK, I successfully implemented the Action Text artifacts.
Moving forward, I've created a JSON API that exposes the resources created through the forms. The output JSON looks like this:
{
"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"
}
PROBLEM:
If you see, the content.body attribute has Rails Action Text tags:
<action-text-attachment sgid...>
In the form at the end of the day the Rails Action Text converts this tag into a figure HTML tag like this in order to render the Images:
<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>
QUESTION:
How can I render the same HTML with the JSON API?
I need to expose this content in order to render it in several client apps.
解决方案 Let's say I have has_rich_text :description
. In order to get pure body
I would need to call it with description.body.to_s
as it gives me:
"<div class=\"trix-content\">\n <div>test</div>\n</div>\n"
However simple description.body
would give me:
#<ActionText::Content "<div class=\"trix-conte...">
这篇关于Rails 6 - 动作文本 &应用程序接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文