views:

64

answers:

2

How do I get only first (say) 200 characters from my object Article (which is in a table articles with aattributes content and title)?

Thank you

+5  A: 

The rails' TextHelper has a truncate method.

So, in your views, you just have to do :

<%= truncate @article.content, :length => 50 %>

Where 50 is the number of characters you want to display.

Damien MATHIEU
+1 because you beat me on time. :)
Simone Carletti
+1  A: 
irb(main):004:0> '0123456789'[3,7]
=> "3456789"
irb(main):005:0> '0123456789'[3..7]
=> "34567"
irb(main):006:0> '0123456789'[3...7]
=> "3456"

Above code and outputs are self explanatory.

Chirantan