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#[]:
http://www.ruby-doc.org/core-1.9.3/String.html#method-i-5B-5D

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

– Matma R.

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

Use String#[]: http://www.ruby-doc.org/core-1.9.3/String.html#method-i-5B-5D

<%= 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.

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-truncate

1 Like

Lars H. wrote in post #1073861:

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

Use String#[]: http://www.ruby-doc.org/core-1.9.3/String.html#method-i-5B-5D

<%= 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.

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-truncate

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs