在我的文本区域中显示降价 [英] displaying markdown in my textarea
问题描述
我正在使用BlueCloth根据用户输入的文本区域的内容从markdown创建html,如下所示:
I'm using BlueCloth to create html from markdown from the content my users enter into a textarea like this:
def create
@post = Post.new(params[:post]) do |post|
body = BlueCloth.new(post.body)
post.body = body.to_html
end
...
end
这很好用!我可以很好地将html存储在数据库中,但是当用户进行编辑时,如何在文本区域中显示markdown?我试过了:
This works great! I get the html stored in the database fine, But how do I show markdown in the textarea when the user edits? I tried:
def edit
@post = Post.find(params[:id])
@post.body = BlueCloth.new(@post.body)
@post.body.text
end
我的文本区域中的输出如下:
The output in my textarea looks like:
#<BlueCloth:0x10402d578>
推荐答案
Bluecloth的文档定义不明确.我不确定是否有转换html => markdown的简单方法.
Bluecloth's documentation isn't very well defined. I'm not sure there's an easy way to convert html => markdown.
但是,没有什么可以阻止您将markdown存储在数据库中,并根据需要将其转换为html.
However, there's nothing stopping you storing the markdown in your database, and convert it to html as necessary.
如果您希望html是@ post.body返回的默认值,那么您始终可以覆盖访问器.
If you want html to be the default returned by @post.body, then you could always override the accessor.
class Post < ActiveRecord::Base
...
def body
BlueCloth.new(@body).to_html
end
def markdown
@body
end
end
现在@ post.body返回markdown的html版本.而@ post.markdown返回markdown源.
Now @post.body returns the html version of the markdown. while @post.markdown returns the markdown source.
这篇关于在我的文本区域中显示降价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!