解决方案
您应该编写自己的验证方法,在其中初始化BlueCloth对象,并尝试调用 to_html
方法捕获任何异常。如果你遇到异常,验证失败,否则它应该没问题。
在你的模特中:
protected:
def validate
bc = BlueCloth.new(your_markdown_string_attribute)
begin
bc.to_html
rescue
errors.add(:your_markdown_string_attribute, 'has invalid markdown syntax')
end
end
其他提示
我做了一些研究并决定使用 RDiscount 而不是BlueCloth。 RDiscount似乎比BlueCloth更快,更可靠。
在您的Rails环境中集成RDiscount很容易。在 environment.rb
中包含以下内容,您就可以开始了:
begin
require "rdiscount"
BlueCloth = RDiscount
rescue LoadError
# BlueCloth is still the our fallback,
# if RDiscount is not available
require 'bluecloth'
end
(使用Rails 2.2.0测试)
不隶属于 StackOverflow