所以我希望能够在帖子的正文中添加链接(而不是以纯文本形式显示)。但是,我不想允许任何其他HTML标记。现在我有:
sanitize @post.body, tags: %w(a), attributes: %w(href)
但这似乎不起作用。 我也试过了
simple_format(@post.body).gsub(URI.regexp, '<a href="\0">\0</a>').html_safe
但是它允许其他HTML标签,我不想要。
任何想法如何解决这个问题?谢谢!
答案 0 :(得分:0)
Ruby / Rails不仅会识别字符串中的链接,因为它中包含http或www somwhere。假设您通过表单获取@post的主体,您需要将输入包装在某种WYSIWYG编辑器中,例如tinymce。然后,如果WYSIWYG编辑器保存序列化输入:
click to see this link about <a href=\"https://google.ca\">google.ca</a>
到数据库,您可以将<a>
标记和href
属性列入白名单,以便实际生成链接
答案 1 :(得分:0)
使用auto_link gem:auto_link(@ comment.body)
结束