Puts if exists?

i have a real simple one today…

here’s how it works.

i have a field called address2 that is in most cases, blank…

the rhtml that does this looks like:

<%= place[:address1] %>
<%= place[:address2] %>
<%= place[:city]
%>, <%= place[:state] %> <%= place[:zip] %>

what i would like to do is only print address 2 if it exists…

otherwise, blank…

is there a class method that does this?

thanks!

If place[:address2] is nil or empty string then <%= place[:address2] %>
should work. But I just noticed those
tags… You’ll probably
want to
do something more like <%= “
#{place[:address2]}
” unless
place[:address2].blank? %>. If you’ve got an aversion to putting the
HTML up
inside the Ruby there, you could write this out as block

<% if place[:address2] -%>

<%= place[:address2] %>

<% end -%>

but it’s so short I’d just stick with embedding the HTML in the
[embedded]
Ruby.

RSL

a helper would be nice for something like this.

def address_block()
address = []
address << block[:address1]
address << block[:address2] unless block[:address2].blank?
address << block[:city] + “, " + block[:state] + " " + block[:zip]
address.join(”
")
end

then in view:

<%= address_block(place) -%>

now your view is nice and clean

Chris H. wrote:

a helper would be nice for something like this.

def address_block()
address = []
address << block[:address1]
address << block[:address2] unless block[:address2].blank?
address << block[:city] + “, " + block[:state] + " " + block[:zip]
address.join(”
")
end

then in view:

<%= address_block(place) -%>

now your view is nice and clean

very nice…

i put all these into my code, and it worked!

thanks, all!

should have been

def address_block(block)