我希望我的应用程序允许用户在其帖子中发布链接,并自动识别这些链接。为此,我一直在使用 auto_link :以下是调用以显示某人的帖子的部分内容:
_post.html.erb:
<tr>
<td >
<span class="post_header"><h4><%= link_to "#{post.user.first_name} #{post.user.last_name}", post.user %></h4></span>
<p> <%= auto_link(post.content) %> </p>
<span class="post_timestamp">
Opined <%= time_ago_in_words(post.created_at) %> ago
</span>
</td>
</tr>
对于单个 post.content 输出以下内容:
<p> Wondering if this link <a href="http://www.economist.com/blogs/freeexchange">http://www.economist.com/blogs/freeexchange</a> will become a proper link
为什么自动链接会创建/转义尖括号到
Wondering if this link <a href="http://www.economist.com/blogs/freeexchange">http://www.economist.com/blogs/freeexchange</a> will become a proper link
最佳答案
在 Rails 3 中,erb 默认不允许任何 ruby 输出包含 html。要解决这个问题,您可以使用 "some string".html_safe
<%= auto_link(post.content).html_safe %>
但是当然任何 html 或 javascript 都将被允许。所以...
<%= sanitize(auto_link(post.content).html_safe) %>
关于ruby-on-rails - 如何阻止 auto_link 转义尖括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6584449/