PMP
PMP

Reputation: 231

Ruby on Rails Max Length in View

So I have my index.html.erb for my /articles and I have a list of all my articles, with its description. So then in the show.html.erb for the articles, the description is 500 characters of length. How do I display in the index.html.erb only 250 characters, then a read more link?

Upvotes: 0

Views: 1202

Answers (2)

Kushal
Kushal

Reputation: 218

it is very simple like this,

<%= article.description[0,250] % >

it will print only first 250 character of description.

Upvotes: 0

Huy
Huy

Reputation: 11206

You can use the truncate method.

So let's say that your description is description.

In your view, you would have something like this followed by a link_to tag to your read more page.

<%= truncate(description, length: 250) %>

Upvotes: 6

Related Questions