如何将 RedCloth.to_html 转换回可编辑的普通文本?
我使用 RedCloth 将纯文本保存在表单中并将其转换为 HTML。例如,在我的表单中写入并保存它,将使其显示与我编写的方式完全相同:
This sentence
gets inserted into it
proper html syntax
to preserve line breakage.
使用此代码:
def parse_code
self.text = RedCloth.new(text).to_html
end
然后我可以使用以下代码重新显示它:
= raw post.text
但是当我想编辑它时,它会返回到我:
<p>This sentence</p>
<p>gets inserted into it</p>
<p>proper html syntax</p>
<p>to preserve line breakage</p>
我怎样才能做到这一点,以便当我编辑它时,它看起来和我保存它之前一样?
谢谢!
I have with RedCloth saved plain text in a form and converted it to HTML. For example, writing this in my form, and saving it, would make it display the exact same way I wrote it :
This sentence
gets inserted into it
proper html syntax
to preserve line breakage.
With this code :
def parse_code
self.text = RedCloth.new(text).to_html
end
And then I can redisplay it with this :
= raw post.text
But when I want to edit it, it it returns to me as :
<p>This sentence</p>
<p>gets inserted into it</p>
<p>proper html syntax</p>
<p>to preserve line breakage</p>
How can I make it, so that when I edit it, it looks the same way it did before I went and saved it ?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会将纺织代码保留在 Textile 中,并仅在视图中转换为 HTML:在
Textile 和 HTML 之间进行转换感觉不是一个好的做法。您的
parse_code
方法似乎导致您的文本转换为 HTML.. 并存储到数据库中。但是,如果您想将 HTML 转换为纺织品,也许 clothred 适合您,或者阅读 此博客。
I would leave the textile code stored in textile and do the conversion to HTML only in the view:
Converting between textile and HTML does not feel to be a good practice. Your
parse_code
method seem that it caused your text to be converted to HTML.. and than stored to the Db.However if you want to convert HTML to textile, maybe clothred is for you or read this blog.
编辑:射击!我误解了这个问题!
您可以使用 ClothRed 将该文本区域的值分配回纺织品:
抱歉!
Edit: Shoot! I misunderstood the question!
You'd assign that text area's value back to textile using ClothRed:
Sorry!
如果我理解正确的话,您正在将 HTML 输出存储在数据库中。不要这样做,而是存储原始 Textile 内容,然后在向用户显示时将其转换为 HTML。
If I understood you right, you are storing the HTML output in the database. Instead of doing that, store the raw Textile contents and then convert them to HTML when showing it to the user.