Truncate & Simple Format String in Ruby on Rails

I am trying to take a string and make it with simple_format, while truncating it. It works when I just use one or the other, but not when I do both. Why it does not do simple_format and truncates at the same time.

controller

myString = "Apple’s New Laptop"

View

<%= simple_format truncate( myString, :length => 20 ) %>
+4
source share
3 answers

The way to do this is truncate after the one you used simple_formatin the line. Since truncate preempts the default string, you should use the option escape: false.

> myString = "Apple&#8217;s New Laptop"

> truncate(simple_format(myString), escape: false) 
> => "<p>Apple&#8217;s New Laptop..."

> truncate(simple_format(myString), escape: false, length: 19)
> => "<p>Apple&#8217;s..."

This can create unbalanced HTML tags, for example, by clipping </p>, so use with caution.

+3

- truncate-helper Rails 4. :

HTML, , : escape is false.

http://apidock.com/rails/v4.0.2/ActionView/Helpers/TextHelper/truncate

. Unicode , , \u . truncate char char.

myString = "Apple\u2019s New Laptop"
+1

It may be late, but useful to someone else. It worked for me.

 <%= truncate(myString, escape: false, length: 20 ) %>
+1
source

Source: https://habr.com/ru/post/1527211/


All Articles