0

要显示一篇文章,我在文章/show.html.haml 中使用以下行

= simple_format(@article.content)

在浏览器中查看结果非常有效。无论如何,我使用前几个词作为搜索引擎的元描述,如果文章的第一行是一个链接,它会在元描述中显示丑陋的 html 代码。例如,我有一篇文章以:

<p> This article is about <%= link_to 'food', 'http://test.com' %> and you can ...

目前我使用以下代码来呈现元描述:

@content = "#{truncate(@article.content, :length => 320)}"

在我的 application.html.erb 我使用:

<meta name="description" content="<%= @content %>">

在标题中它显示如下:

<meta content="<p> This article is about <%= link_to 'food', 'http://test.com' %> and you can ..." name="description">

如何确保内容仍然在浏览器中正确显示并且元描述正确,意味着:

“这篇文章是关于食物的,你可以……”

4

1 回答 1

1

只是一个想法,您可以尝试使用例如 strip_tags 方法

truncate(strip_tags(@article.content), :length => 320)

未经测试,但它应该可以工作,更多关于strip_tags

于 2013-09-11T10:57:50.247 回答