我使用 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>
我怎样才能做到这一点,以便当我编辑它时,它看起来和我保存之前一样?
谢谢!
最佳答案
我会将纺织代码保留在纺织中,并仅在 View 中转换为 HTML:
= raw RedCloth.new(@post.text).to_html
在 Textile 和 HTML 之间进行转换感觉不是一个好的做法。您的 parse_code
方法似乎导致您的文本转换为 HTML.. 并存储到数据库中。
关于ruby-on-rails - 如何将 RedCloth.to_html 转换回可编辑的普通文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4391459/