2011-02-10 48 views
4

我想爲我截斷的ruby字符串省略「...」鏈接。下面是我有:在ruby中省略截斷鏈接

<%= truncate(testimony.testimony, :length => 125, :omission => (link_to "...", testimony)) %><br /> 

,但它這樣做:

Etiam porta sem malesuada magna mollis euismod. Aenean lacinia bibendum nulla sed consectetur<a href="/testimonies/1">...</a> 

而不是使實際...鏈接它顯示的代碼。請參閱:http://cl.ly/4Wy3獲取屏幕截圖。

謝謝!

回答

9

的問題是,截斷santizes輸出,你需要使用raw()如下文檔:

結果沒有被標記爲HTML安全的,所以會受到默認在視圖中使用時除外,除非由raw()包裝。如果文本包含HTML標籤或實體,應該小心,因爲截斷可能會產生無效的HTML(如不平衡或不完整的標籤)。

編輯例如:

<%= raw(truncate(testimony.testimony, :length => 125, :omission => (link_to "...", testimony))) %><br /> 
4
<%= truncate(testimony.testimony, :length => 125, :omission => "%s") % link_to("...", testimony) %> 
+0

酷是什麼 「%S」)%是什麼意思? – Marc 2011-02-11 00:02:51

+0

其實它仍然出現如下:Etiam porta sem malesuada magna mollis euismod。 Aenean lacinia bibendum nulla sed consectetur ... Marc 2011-02-11 00:04:25