Jump to content

The ultimate community for Ruby on Rails developers.


Photo

How to add a word limit for ruby script?

ruby

  • Please log in to reply
5 replies to this topic

#1 xuanyinwen

xuanyinwen

    Passenger

  • Members
  • 2 posts

Posted 04 September 2013 - 10:04 PM

my original script is:

<%= property.advert_heading ? link_to(property.advert_heading, user_property_path(user,property)): ' '-%>


I want to add a script " length =>120" to limit the text to 120. how should I do it please



#2 Rowel

Rowel

    Controller

  • Members
  • 109 posts

Posted 05 September 2013 - 12:04 AM

mystring = 'some-string-here'
length = 120

mystring[0, length]    # returns the first 120 characters


#3 Rowel

Rowel

    Controller

  • Members
  • 109 posts

Posted 05 September 2013 - 01:42 AM

or in the model, add 

validates :yourtextfield, presence: true, length: {maximum: 120}


#4 Ohm

Ohm

    Guard

  • Members
  • 186 posts
  • LocationCopenhagen

Posted 05 September 2013 - 05:32 AM

... or you could truncate the value

truncate property.advert_heading, length: 120

http://api.rubyonrai...thod-i-truncate


  • lazarus likes this

Blog: http://ohm.sh | Twitter: madsohm


#5 xuanyinwen

xuanyinwen

    Passenger

  • Members
  • 2 posts

Posted 13 September 2013 - 03:28 AM

HI there, thanks for reply,

 

I still can't work it out, first I won't be able to edit the model, I know I can use truncate:

<%= truncate(property.advert_heading, :length =>120) -%>

but what I can do with the link to, it won't work if I make it like:

<%= truncate(property.advert_heading, :length =>120) ? link_to(property.advert_heading, user_property_path(user,property)): ' '-%>


#6 lazarus

lazarus

    Signalman

  • Members
  • 16 posts
  • LocationGreece

Posted 01 October 2013 - 06:11 PM

Use it this way:

 

<%= link_to(truncate(property.advert_heading, :length =>120), user_property_path(user,property)) %>







Also tagged with one or more of these keywords: ruby

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users