Wednesday, 9 April 2014

Rails Truncate method

Today i would like to give some information on Rails api Truncate method.
I was looking for a  method in my Ruby on Rails project which can truncate a string  after some characters.. after searching I found 'truncate' method, which is a rails api method for truncating string in views.
So if you have string
str= "Hi, we are going to truncate any large string by this rails api method??" # 72 characters

the truncate method can be used in the view like this:

truncate(str, :length => 25)

and the result would be :
Hi, we are going to tr...

This was very useful since we had to write seprate ruby methods for truncating.