2013-04-29 73 views
0

要顯示的文章中,我用在文章下面的行/ show.html.hamlsimple_format Rails中顯示醜陋的HTML代碼中的meta描述

= simple_format(@article.content) 

查看結果在broswer完美。無論如何,我將前幾個單詞用作搜索引擎的元描述,並且如果文章的第一行是例如它在元描述中顯示難看的html代碼的鏈接。例如。我有一個開頭一篇文章:

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

目前我使用下面的代碼來呈現meta描述:

@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"> 

我怎麼能保證的是,在瀏覽器中正確的內容仍然顯示和meta描述是否正確,意思是:

「這篇文章是關於食物,你可以......」

回答

1

只是一個想法,您可以嘗試使用例如用strip_tags方法

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

沒有測試,但它應該工作,詳細瞭解strip_tags