ruby-on-rails - 如何将 RedCloth.to_html 转换回可编辑的普通文本?

标签 ruby-on-rails editor text-editor redcloth

我使用 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.. 并存储到数据库中。

但是,如果您想将 HTML 转换为纺织品,也许 clothred适合您或阅读this blog .

关于ruby-on-rails - 如何将 RedCloth.to_html 转换回可编辑的普通文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4391459/

相关文章:

.net - 堆栈溢出时的编辑控制

python - 使用 GTK TextView 撤消

ruby-on-rails - 在模型类方法上运行辅助方法

ruby-on-rails - 设计在身份验证失败时使用 JSON 请求登录不起作用

eclipse - 如何在 VIm 中使用 Eclipse 格式化程序配置文件?

java - Intellij IDEA 2016.1.3 JAVA语句被删除

java - 如何添加新版本的 Apache Tomcat 来配置新服务器?

c# - 如何使用 WPF 构建一个简单的源代码编辑器?

ruby-on-rails - Ruby on Rails Web 应用程序的最佳 CI 服务器是什么?

java - 是否有一个 Grails 插件用于检测 N+1 抓取(类似于 RailsBullet gem)?