我正在创建一个Rails 6.0.0应用程序,它有以下模块:
- Web-表单/Cruds
- Api
有一个CRUD,我使用Action Text。表单工作正常,我成功地实现了Action Text工件。
接下来,我创建了一个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"
}
问题:
如果你看到了,content.body属性有Rails Action Text标签:
<action-text-attachment sgid...>
在一天结束时的表单中,Rails Action Text将此标记转换为这样的图形HTML标记,以便呈现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>
问题:
如何使用JSON API呈现相同的HTML?
我需要公开此内容,以便在几个客户端应用程序中呈现它。
所以我制作了一个伪列,用Action Text的原始输出填充模型的before_save
回调。
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。
但是,如果要迁移旧记录,则必须手动创建操作文本记录,否则将出现非常的持续undefined method 'render_action_text_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
拯救自己一个充满伤害的世界。
假设我有has_rich_text :description
。为了得到纯body
,我需要用description.body.to_s
来称呼它,因为它给了我:
"<div class="trix-content">n <div>test</div>n</div>n"
无论简单的description.body
会给我什么:
#<ActionText::Content "<div class="trix-conte...">