2

In my inbox view, where I am able to list each individual message, I want to display the first 10 characters of a message if the message is larger than 10 characters or display the entire message. The user can then click on the message to view the entire message.

message.body

is where the contents of the message are stored in the database.

pratski
  • 1,448
  • 2
  • 22
  • 37

3 Answers3

8

Try this:

truncate(message.body, :length => 10)
Dave Sexton
  • 10,768
  • 3
  • 42
  • 56
6

Use truncate. Link for the documentation: http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-truncate

MurifoX
  • 14,991
  • 3
  • 36
  • 60
1

To have a string with a total of 10 chars (including the ... at the end)

message.body.truncate(10)

or to cut to end of last complete word

message.body.truncate(10, separator: /\s/)

Examples:

"some simple words here that is too long".truncate(23)
 => "some simple words he..." 

"some simple words here that is too long".truncate(23,separator: /\s/)
 => "some simple words..."
Aryeh Beitz
  • 1,974
  • 1
  • 22
  • 23