Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate + Sanitize in Rails Views

I ran into a small problem today when I was trying to use sanitize and truncate in conjunction with one another to create an excerpt for my blog. Each of the methods worked by itself, but used together it would only truncate. I tried both of these syntaxes (the former being recommended in a blog post titled "Six Ruby on Rails Tips & Tricks"):

<%= truncate(sanitize(post.content), length: 580) %>
<%= sanitize(truncate(post.content, length: 580, separator: '<p>')) %>

And then I tried to put truncate in the controller, and sanitized that object in the view, but still no.

Finally I got it to work like this:

<%= sanitize(post.content.truncate(580, separator: '</p>')) %>

What I'd like to know is why didn't it work when I wrapped a method in another method? What's the difference with the last way I tried it?

TIA 'bondibox'

like image 915
user2649201 Avatar asked Aug 03 '13 18:08

user2649201


1 Answers

This worked for me:

<%=  sanitize(article.description[0..100]) %>
like image 137
Ruben Portz Avatar answered Oct 19 '22 18:10

Ruben Portz