AllieCat AllieCat - 5 months ago 18
HTML Question

Truncate string in Rails: "..." showing up on strings at length

I'm currently trying to truncate any strings greater than 65 characters.

My code is

<title><%= truncate(title.html_safe, length:65) %></title>


It works great for titles longer than 65 characters. But titles that are exactly 65 character still get truncated.

For example:


title:"This post is exactly 56 characters characters characters
characte"

shows on page as "This post is exactly 56 characters characters
characters chara..."


Should I not be using truncate?

Answer

truncate is the right method. This might be a bug in your version of rails? Here's what I get on my console:

[5] pry(main)> helper.truncate("This post is exactly 56 characters characters characters characte", length: 65)
=> "This post is exactly 56 characters characters characters characte"
[6] pry(main)> helper.truncate("This post is exactly 56 characters characters characters characte", length: 64)
=> "This post is exactly 56 characters characters characters char..."

I'm running Rails 4.0.4 in this example.

Comments