Note that there are some explanatory texts on larger screens.

plurals
  1. PORails truncate UTF-8 strings containing é (for example)
    primarykey
    data
    text
    <p>I am working on a <em>rails 3.1</em> app with <em>ruby 1.9.3</em> and <em>mongoid</em> as my ORM. I am facing an annoying issue. I would like to truncate the content of a post like this:</p> <pre><code>&lt;%= raw truncate(strip_tags(post.content), :length =&gt; 200) %&gt; </code></pre> <p>I am using <code>raw</code> and <code>strip_tags</code> because my <code>post.content</code> is actually handled with a rich text editor. </p> <p>I have a serious issue with non ASCII characters. Imagine my post content is the following:</p> <pre><code>éééé éééé éééé éééé éééé éééé éééé éééé </code></pre> <p>What I am doing above in a naive way does this:</p> <pre><code>éééé éééé éééé éééé éééé &amp;eac... </code></pre> <p>Looks like truncate is seeing every word of the string like <code>&amp;eacute;&amp;eactute;&amp;eacute;&amp;eacute;</code>.</p> <p>Is there a way to either:</p> <ol> <li><strong>Have truncate handle an actual UTF-8 strings</strong>, where 'é' stands for a single character ? That would be my favorite approach.</li> <li>Hack the above instruction such that the result is better, like force rails to truncate between 2 words,</li> </ol> <p>I am asking this question because I have not found <strong>any</strong> solution so far. This is the only place in my app where I have problems with such character, and it is a major issues since the whole content of the website is in french, so contains a lot of <code>é, ç, à, ù</code>.</p> <p>Also, I think this behavior is quite unfortunate for the <code>truncate</code> helper because in my case it does not truncate 200 characters at all, but approximately 25 characters !</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload