如何将 RedCloth 添加到 form_for 中?
一个令人尴尬的问题,但我似乎无法将文档翻译为实际的 form_for。这就是该网站提供的全部内容。
RedCloth.new("Some text").to_html
#=> "<p>Some text</p>"
我知道这就是我保存后解析它的方式。但如何将其另存为标记文本呢?
这是我开始的尝试,但我不知道如何设置参数以将文本区域保存为 RedCloth。有什么想法吗?
- form_for @text do |f|
# some RedCloth instantiation
f.submit
An embaressing question, but I can't seem to translate the documentation to an actual form_for. This is all the site provides..
RedCloth.new("Some text").to_html
#=> "<p>Some text</p>"
I get that that's how I parse it after its been saved. But how do I save it as marked up text?
Here's my attempt at beginning this, but I don't know how to set the parameter to save the textarea as RedCloth. Any ideas?
- form_for @text do |f|
# some RedCloth instantiation
f.submit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不会像那样保存解析为 RedCloth 的参数,我也不推荐它。将其解析为 RedCloth 将导致原始值丢失,除非您将输出存储在备用字段中,这正是我所推荐的。
您可以在模型中使用
before_save
来解析该值并存储它:当您想要在视图中渲染
parsed_text
值时,您必须告诉 Rails 它是这样做是安全的:但是,这里包含的代码不考虑人们混合 Markdown 和 HTML,所以要非常小心如何使用它。
You don't save the parameter parsed as RedCloth like that, nor would I recommend it. Parsing it into RedCloth will result in the original value being lost unless you stored the output in an alternate field, which is what I would recommend.
You can use a
before_save
in your model to parse that value and store it:When you want to render the
parsed_text
value in your view you'll have to tell Rails that it's safe by doing this:However, the code contained here does not account for people mixing Markdown and HTML, so be very careful how you use it.