Sunday, October 24, 2010

HTML stripping, Truncating string in Ruby, Rails

For Truncating String I have used 'truncate' rails helper method.
limiting to 10 characters
truncate("im srikanth glad to meet you", 10)
>> "im srik..."
For HTML stripping, I have used gsub..
str.gsub(/<\/?[^>]*>/, "")


Timur said...

In Rails 3.0 you have a helper strip_tags. Example: you have a product and you want to show the description you do it in a line of code
<%= truncate(strip_tags(product.description), :length => 80) %>

By the way you can use SyntaxHighlighter to make the code from your blogspot pretty.

walsh259 said...

Thanks, i tried many options but this finally worked.
<%= truncate(strip_tags(product.description), :length => 80) %>