如何将 RedCloth 添加到 form_for 中?

发布于 2024-10-05 06:05:54 字数 345 浏览 5 评论 0原文

一个令人尴尬的问题,但我似乎无法将文档翻译为实际的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风筝有风,海豚有海 2024-10-12 06:05:54

您不会像那样保存解析为 RedCloth 的参数,我也不推荐它。将其解析为 RedCloth 将导致原始值丢失,除非您将输出存储在备用字段中,这正是我所推荐的。

您可以在模型中使用 before_save 来解析该值并存储它:

before_save :parse_text

# your model methods go here

private

  def parse_text
    self.parsed_text = RedCloth.new(text).to_html
  end

当您想要在视图中渲染 parsed_text 值时,您必须告诉 Rails 它是这样做是安全的:

@object.parsed_text.html_safe

但是,这里包含的代码不考虑人们混合 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:

before_save :parse_text

# your model methods go here

private

  def parse_text
    self.parsed_text = RedCloth.new(text).to_html
  end

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:

@object.parsed_text.html_safe

However, the code contained here does not account for people mixing Markdown and HTML, so be very careful how you use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文