Hi! I''m writing a piece of code that tries to truncate the amount of text displayed in the cell to restrict it such that the formatting doesn''t spoil if the string is too long. What I want to do is: 1. If the ''met_person'' string is longer than 20 characters, I want to pick the first 18 and add "..." to it and display that in the cell. 2. Also, I want to use the title attribute for the cell so that a mouse over shows the full text. 3. If the name is shorter than 20 characters, it is fine - just display it completely So, right now, I have something like this which works (I guess it can be cleaned up): <td class=''person'' title=''<%= meeting.met_person %>''><%= (meeting.met_person.length > 20)? meeting.met_person[0..17]+''...'':meeting.met_person %></td> My questions: 1. I''d like to make this a generic helper that automatically truncates the text. So, should I put in a helper file? So, this could be something like truncate_text(meeting.met_person, 20) and the function could do the check and return the correct string. 2. Since there is sufficient repetition to create the title and the text in the cell, it may be nice to create something that creates the full HTML code, rather than just the string. Where would that go? Would that be a helper too? Thanks Mohit. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Mohit, There is already a truncate TextHelper. I believe you do something like... <%= truncate(meeting.met_person, 20) %> Regards, Brian On Sep 27, 2006, at 11:18 AM, Mohit Sindhwani wrote:> > Hi! I''m writing a piece of code that tries to truncate the amount of > text displayed in the cell to restrict it such that the formatting > doesn''t spoil if the string is too long. > > What I want to do is: > 1. If the ''met_person'' string is longer than 20 characters, I want to > pick the first 18 and add "..." to it and display that in the cell. > 2. Also, I want to use the title attribute for the cell so that a > mouse > over shows the full text. > 3. If the name is shorter than 20 characters, it is fine - just > display > it completely > > So, right now, I have something like this which works (I guess it > can be > cleaned up): > <td class=''person'' title=''<%= meeting.met_person %>''><%> (meeting.met_person.length > 20)? > meeting.met_person[0..17]+''...'':meeting.met_person %></td> > > My questions: > 1. I''d like to make this a generic helper that automatically truncates > the text. So, should I put in a helper file? So, this could be > something like truncate_text(meeting.met_person, 20) and the function > could do the check and return the correct string. > > 2. Since there is sufficient repetition to create the title and the > text > in the cell, it may be nice to create something that creates the full > HTML code, rather than just the string. Where would that go? Would > that be a helper too? > > Thanks > Mohit. > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
For the repetitive html snippet you may write a partial, the partial could then use the truncate helper. On 9/27/06, Mohit Sindhwani <mo_mail-RxrYI66vbj0AvxtiuMwx3w@public.gmane.org> wrote:> > > > 2. Since there is sufficient repetition to create the title and the text > in the cell, it may be nice to create something that creates the full > HTML code, rather than just the string. Where would that go? Would > that be a helper too? > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Ouch! I thought I had seen that in the list, but couldn''t find it in the emails I saved, so I read up on the String class and made my own. I shall take a look at that - thanks! Cheers Mohit. Brian DeWeese wrote:> Mohit, > > There is already a truncate TextHelper. > > I believe you do something like... > <%= truncate(meeting.met_person, 20) %> > > Regards, > Brian > > On Sep 27, 2006, at 11:18 AM, Mohit Sindhwani wrote: > >> >> Hi! I''m writing a piece of code that tries to truncate the amount of >> text displayed in the cell to restrict it such that the formatting >> doesn''t spoil if the string is too long. >> >> What I want to do is: >> 1. If the ''met_person'' string is longer than 20 characters, I want to >> pick the first 18 and add "..." to it and display that in the cell. >> 2. Also, I want to use the title attribute for the cell so that a mouse >> over shows the full text. >> 3. If the name is shorter than 20 characters, it is fine - just display >> it completely >> >> So, right now, I have something like this which works (I guess it can be >> cleaned up): >> <td class=''person'' title=''<%= meeting.met_person %>''><%= >> (meeting.met_person.length > 20)? >> meeting.met_person[0..17]+''...'':meeting.met_person %></td> >> >> My questions: >> 1. I''d like to make this a generic helper that automatically truncates >> the text. So, should I put in a helper file? So, this could be >> something like truncate_text(meeting.met_person, 20) and the function >> could do the check and return the correct string. >> >> 2. Since there is sufficient repetition to create the title and the text >> in the cell, it may be nice to create something that creates the full >> HTML code, rather than just the string. Where would that go? Would >> that be a helper too? >> >> Thanks >> Mohit. >> >> >> >> >> >> >>--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Hi Nasir, That certainly answers that question! Thanks guys! I had sorta forgotten that the partial could use a helper.. Cheers Mohit. Nasir Khan wrote:> For the repetitive html snippet you may write a partial, the partial > could then use the truncate helper. > > On 9/27/06, *Mohit Sindhwani* < mo_mail-RxrYI66vbj0AvxtiuMwx3w@public.gmane.org > <mailto:mo_mail-RxrYI66vbj0AvxtiuMwx3w@public.gmane.org>> wrote: > > > > 2. Since there is sufficient repetition to create the title and > the text > in the cell, it may be nice to create something that creates the full > HTML code, rather than just the string. Where would that go? Would > that be a helper too? >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---