如何在 ruby on Rails 中清理用户生成的 html 代码
我将用户生成的 html 代码存储在数据库中,但某些代码已损坏(没有结束标记),因此当此代码会弄乱页面的整个渲染时。
我如何使用 ruby on Rails 来防止这种行为。
谢谢
I am storing user generated html code in the database, but some of the codes are broken (without end tags), so when this code will mess up the whole render of the page.
How could I prevent this sort of behaviour with ruby on rails.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(4)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
使用适当的 HTML 解析器(例如 Nokogiri )来执行此操作并不难,它可以作为处理方法的一部分执行清理操作:
一旦正确解析,您应该拥有完全平衡的标签。
It's not too hard to do this with a proper HTML parser like Nokogiri which can perform clean-up as part of the processing method:
Once parsed properly, you should have fully balanced tags.