Truncate by Word Count in Rails
Rails has a convenient method for truncating strings based on word count.
my_string = "Hello World you are now reading a til post" my_string.truncate_words(2) #=> "Hello World..."
The method automatically adds
... to the end of the string to indicate that the string has been shortened. You can customize this omission by passing an omission argument.
my_string.truncate_words(2, omission: "... (read more)") #=> "Hello World... (read more)"