String in rails and ruby show first n character

I am just wondering how can i show the first 200 character of a string ?
with rails. My application his has follow

<%= f.description %>
I have try the follow

<%= f.description(200).to_s %>
But it doesn’t work. Thanks in advance and description is a text area

Use String#[]:

<%= f.description[0, 200] %>

– Matma R.

On 08/29/2012 08:34 PM, Bartosz Dziewoński wrote:

Use String#[]: Class: String (Ruby 1.9.3)

<%= f.description[0, 200] %>

In Rails, you can also utilize the truncate helper method:

<%= truncate(f.description, :length => 200, :separator => ’ ') %>

This will ensure the string is not chopped off in the middle of a word.

1 Like

Lars H. wrote in post #1073861:

On 08/29/2012 08:34 PM, Bartosz Dziewoński wrote:

Use String#[]: Class: String (Ruby 1.9.3)

<%= f.description[0, 200] %>

In Rails, you can also utilize the truncate helper method:

<%= truncate(f.description, :length => 200, :separator => ’ ') %>

This will ensure the string is not chopped off in the middle of a word.

Thanks both works, didn’t know about both of them