Marc
Marc

Reputation: 2584

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!

Upvotes: 4

Views: 2227

Answers (2)

macarthy
macarthy

Reputation: 3069

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 />

Upvotes: 10

Simone Carletti
Simone Carletti

Reputation: 176382

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

Upvotes: 4

Related Questions