0

我正在使用 Rinku rails gem 将 URL 链接到我网站上发布的任何内容中。

我已经安装并实现了它,但问题是 HTML 代码显示在视图中。

这是我正在使用的代码

<%= truncate(Rinku.auto_link(feed_item.content), :length=>400, :omission=>' ...(next page)') %>

这是我网站上的示例输出视图

hey guys check out my website at <a href="http://www.someURL.com">www.someURL.com</a>

如您所见,HTML<a>标记显示。feed_item.content 应该更改为什么才能正常工作?

Feed_item.content 是文本,而不是字符串。Rinku 似乎解析了字符串。这是问题的原因吗?

4

1 回答 1

1

The issue is that by default, Rails will escape any html in a string you output in erb (using <%= %>) as a precautionary measure. (If you look at the page source, you should see that your output looks like &lt;a ...&gt; instead of <a ...>.)

To stop this from happening, you can use the .html_safe method to mark that the string is safe to print as html:

<%= truncate(...).html_safe %>
于 2013-03-24T23:57:14.293 回答