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 65 characters characters characters characte"
shows on page as "This post is exactly 65 characters characters characters chara..."
Should I not be using truncate?
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.
If you are using
rails 4.2
or above then you can usetruncate_words
method.
<title><%= (title.html_safe).truncate_words(5) %></title>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With