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/.
Don't put view code (HTML) in your models!
Better to put that in a helper.
--
-- Tom Mornini
I'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
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
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: rubyonra...@googlegroups.com
[mailto:rubyonra...@googlegroups.com] On Behalf Of Gareth Adams
Sent: woensdag 23 augustus 2006 12:23
To: rubyonra...@googlegroups.com
Subject: [Rails] Re: cleaner way to write this...
On 8/23/06, David C. <rails-mai...@andreas-s.net> 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!
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.
Nice. Thanks Gareth!
Almost 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!!
def full_address
addr = [first_name + ' ' + last_name,
address1,
address2,
city + ' ' + state + ', ' + zip].compact
addr.delete_if {|x| x.blank? }
end
thx.