Within the last few days I saw an excellent example of this but can''t find the link. So... I thought I''d ask the experts. In my addressbook model, I wanted to be able to just say thing.full_address to return a preformatted version of the customers entire address. Something like this: # from addressbook model def full_address full = first_name + '' '' + last_name + ''<br>'' + address1 + ''<br>'' unless address2.blank?; full += address2 + "<br>"; end full += city + '' '' + state + '','' + zip full end I''m sure there''s a cleaner more compact way to do this, but this is the extent of my ruby knowledge at this point. Any help cleaning / compacting this would be greatly appriciated. Thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Aug 22, 2006, at 5:52 PM, David C. wrote:> Within the last few days I saw an excellent example of this but can''t > find the link. So... I thought I''d ask the experts. > > In my addressbook model, I wanted to be able to just say > thing.full_address to return a preformatted version of the customers > entire address. Something like this: > > # from addressbook model > def full_address > full = first_name + '' '' + last_name + ''<br>'' + address1 + ''<br>'' > unless address2.blank?; full += address2 + "<br>"; end > full += city + '' '' + state + '','' + zip > full > end > > I''m sure there''s a cleaner more compact way to do this, but this is > the > extent of my ruby knowledge at this point. > > > Any help cleaning / compacting this would be greatly appriciated.Don''t put view code (HTML) in your models! Better to put that in a helper. -- -- Tom Mornini --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Tom Mornini wrote:> On Aug 22, 2006, at 5:52 PM, David C. wrote: > > > Within the last few days I saw an excellent example of this but can''t > > find the link. So... I thought I''d ask the experts. > > > > In my addressbook model, I wanted to be able to just say > > thing.full_address to return a preformatted version of the customers > > entire address. Something like this: > > > > # from addressbook model > > def full_address > > full = first_name + '' '' + last_name + ''<br>'' + address1 + ''<br>'' > > unless address2.blank?; full += address2 + "<br>"; end > > full += city + '' '' + state + '','' + zip > > full > > end > > > > I''m sure there''s a cleaner more compact way to do this, but this is > > the > > extent of my ruby knowledge at this point. > > > > > > Any help cleaning / compacting this would be greatly appriciated. > > Don''t put view code (HTML) in your models! > > Better to put that in a helper. > > -- > -- Tom MorniniI''m with Tom on this one. Make a partial that properly formats the address when passed the object. FWIW, the format I think you were looking for is like this... def print_stuff "#{variable1}-#{variable2}" end _Kevin www.sciwerks.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 8/23/06, David C. <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > > Within the last few days I saw an excellent example of this but can''t > find the link. So... I thought I''d ask the experts. > > In my addressbook model, I wanted to be able to just say > thing.full_address to return a preformatted version of the customers > entire address. Something like this: > > # from addressbook model > def full_address > full = first_name + '' '' + last_name + ''<br>'' + address1 + ''<br>'' > unless address2.blank?; full += address2 + "<br>"; end > full += city + '' '' + state + '','' + zip > full > end > > > I''m sure there''s a cleaner more compact way to do this, but this is the > extent of my ruby knowledge at this point. > > > Any help cleaning / compacting this would be greatly appriciated. > > Thanks!One thing I''d suggest is writing a function in the model like this: def full_address [first_name + '' '' + last_name, address1, address2, city + '' '' + state + '','' + zip].compact # compact removes nil values from an array end and then a helper def format_address(object) object.respond_to?("full_address") and object.full_address.join("<br>") end Gareth --~--~---------~--~----~------------~-------~--~----~ 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 this I would prefer a view helper method because I don''t like html code in my model. def full_address(aAddress) returning html = [] do html << aAddress.first_name + '' '' + aAddress.last_name + ''<br>'' + aAddress.address1 + <br>'' html << aAddress.address2 + "<br>" if !aAddress.address2 html << aAddress.city + '' '' + aAddress.state + '','' + aAddress zip end.join("\n") end Onno -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Gareth Adams Sent: woensdag 23 augustus 2006 12:23 To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: cleaner way to write this... On 8/23/06, David C. <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > > Within the last few days I saw an excellent example of this but can''t > find the link. So... I thought I''d ask the experts. > > In my addressbook model, I wanted to be able to just say > thing.full_address to return a preformatted version of the customers > entire address. Something like this: > > # from addressbook model > def full_address > full = first_name + '' '' + last_name + ''<br>'' + address1 + ''<br>'' > unless address2.blank?; full += address2 + "<br>"; end > full += city + '' '' + state + '','' + zip > full > end > > > I''m sure there''s a cleaner more compact way to do this, but this isthe> extent of my ruby knowledge at this point. > > > Any help cleaning / compacting this would be greatly appriciated. > > Thanks!This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Gareth Adams wrote:> def full_address > [first_name + '' '' + last_name, > address1, > address2, > city + '' '' + state + '','' + zip].compact # compact removes nil > values from an array > end > > and then a helper > > def format_address(object) > object.respond_to?("full_address") and > object.full_address.join("<br>") > end > > GarethNice. Thanks Gareth! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Gareth Adams wrote:> def full_address > [first_name + '' '' + last_name, > address1, > address2, > city + '' '' + state + '','' + zip].compact # compact removes nil > values from an array > endAlmost worked. address2.nil? returns false, but address2.blank? returns true. Since address2 isn''t nil, it still shows. Thanks for pointing me in the right direction!! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
FYI, Added the delete_if to get rid of the blank ones: def full_address addr = [first_name + '' '' + last_name, address1, address2, city + '' '' + state + '', '' + zip].compact addr.delete_if {|x| x.blank? } end thx. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---