Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make omission in ruby truncate a link

I'd like to make the omission "..." a link for my truncated ruby string. Here's what I have:

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

but it does this:

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

Rather than making the actual ... a link it shows the code. See: http://cl.ly/4Wy3 for the screenshot.

Thanks!

like image 643
Marc Avatar asked Dec 10 '22 11:12

Marc


1 Answers

The problem is that truncate santizes the output , you need to use raw() as in the docs below:

The result is not marked as HTML-safe, so will be subject to the default escaping when used in views, unless wrapped by raw(). Care should be taken if text contains HTML tags or entities, because truncation may produce invalid HTML (such as unbalanced or incomplete tags).

EDIT example:

<%= raw(truncate(testimony.testimony, :length => 125, :omission => (link_to "...", testimony))) %><br />
like image 53
macarthy Avatar answered Dec 28 '22 18:12

macarthy