Reputation: 1488
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.
Upvotes: 2
Views: 3242
Reputation: 2078
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..."
Upvotes: 1
Reputation: 15089
Use truncate
. Link for the documentation: http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-truncate
Upvotes: 6