【发布时间】:2012-07-20 18:48:20
【问题描述】:
显然,对用户生成的评论内容使用 html_safe 方法确实不是一个好的解决方案。然而,到目前为止,这是我能想出的实现以下功能的唯一解决方案:我想让用户能够引用另一个评论,只需在评论表单中输入另一个评论的迭代 id,就像这样“#14 "(引用那篇文章的评论 14)。然后这个 #14 在内容输出中被替换为 "[quoted_comment.content]"。
这是我在评论模型中的代码:
def content_with_quotes
if content.match(/(#([0-9]+))\s/)
comment_content = content
comment_content.scan(/(#([0-9]+))\s/) do
if quoted_comment = Comment.where(article_id: self.article_id).where(iteration_id: $2).first
if quoted_comment.created_at < self.created_at
return comment_content.sub(/(#[0-9]+)\s/, "<i>'#{quoted_comment.content}'</i> ")
end
end
end
else
return content
end
end
然后在我的评论视图中,我将它与 comment.content_with_quotes.html_safe 一起应用,一切正常。
所以,这就是我想要的,而且它有效,但当然,这种 html_safe 方法对于用户提交的内容来说是个坏主意,因为它可能不是 html 安全的。
关于如何在不使用 html_safe 方法的情况下使用我的功能的任何建议?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 html-safe