NEWBIE remove empty fields on Show screen

Warning - another newbie question - feel free to assume absolute
ignorance. /grin/

I’m working on some customer managment screens right now. Customers can
have many lines in their address:
Customer
Business and Technology Park
1 Main Street
Suite 300
City, ST Zip+4

The model/table has (address_1, address_2, address_3). But it’s more
common just to see one line:
Customer
1 Main Street
City, ST Zip+4

On the Show screen, I’d like to see it shown as I just typed it above
instead of seeing all the empty lines (with
's) after them:
Customer
1 Main Street

City, ST Zip+4

What’s the “elegant” way to do that?

Thank you!

don’t know, if it’s the perfect way, but i would do it something like
that:

<%= “#{@customer.address_1”}
" if @customer.address_1 -%>
<%= “#{@customer.address_2”}
" if @customer.address_2 -%>
<%= “#{@customer.address_3”}
" if @customer.address_3 -%>

Thorsten M. wrote:

don’t know, if it’s the perfect way, but i would do it something like
that:

<%= “#{@customer.address_1”}
" if @customer.address_1 -%>
<%= “#{@customer.address_2”}
" if @customer.address_2 -%>
<%= “#{@customer.address_3”}
" if @customer.address_3 -%>

That will work. Thank you very much.

I was trying: <%= (@customer.address_1 + ‘
’) if
@customer.address_1 -%>

Which didn’t work correctly, of course. Would you mind explaining why
“#” is at beginning and why the double-quotes are places where they are?

Thank you very much for your help and your patience.

sure, easy enough:

you have a string, then you can place ruby-stuff in it with #{}

so if you have a var @foo=‘text’

then

  • #{@foo}

  • will end up as “
  • text
  • in the above case it’s just an easy way to have the ‘whole thing’ in one
    call
    and it keeps long string concatinations more readable
    (but your try looks good enough to me, i don’t see, why it should not
    work)

    just see a typing error, that’s the correct version (one double-quote
    less within the curly braces):

    <%= “#{@customer.address_1}
    ” if @customer.address_1 -%>

    Denise R. wrote:

    Thorsten M. wrote:

    <%= “#{@customer.address_3”}
    " if @customer.address_3 -%>

    OK… So, that syntax didn’t work for me.

    I changed a little and this does work:

    <%= (customer.address_1 + “
    ”) if customer.address_1 -%>

    But, when I say it “works” I only mean that it will run and display a
    page. I still have the line breaks for some reason.

    Is there a way to give two options? Maybe I could try:

    if customer.address_1.empty?
    do this
    otherwise do this

    /shrug/ Just a thought. But maybe it’s just my syntax…

    And thanks for the syntax explanation, by the way. Every little syntax
    thing I can learn is such a big help right now. :slight_smile:

    For some reason, it still gives me the breaks.

    Here’s what I have:

    <%= link_to company[:name], :action => :edit, :id => @company %>

    <%= "#{@company.address_1}
    " if @company.address_1 -%> <%= "#{@company.address_2}
    " if @company.address_2 -%> <%= "#{@company.address_3}
    " if @company.address_3 -%> <%= company.city %> , <%= company.state %> <%= company.zip %>-<%= company.zip4 %>

    And here’s what I end up with:

    Advanced Opitronic Devices (China) Co., Ltd.
    East YuQing Street
    High-Tech Zone

    Weifang , Shandong 261061-

    On the other hand, this works, but it’s ridiculously verbose:

    <% if company.address_1.empty? %>
    <%else%>
    <%= “#{@company.address_1}
    ” %>
    <% end %>

    <% if company.address_2.empty? %>
    <%else%>
    <%= “#{@company.address_2}
    ” %>
    <% end %>

    <% if company.address_3.empty? %>
    <%else%>
    <%= “#{@company.address_3}
    ” %>
    <% end %>

    <% if company[:state].empty? %>
    <%= company[:city] %>
    <% else %>
    <%= (company[:city] + ', ’ + company[:state]) %>
    <% end %>

    <% if company[:zip4].empty? %>
    <%= company[:zip] %>
    <% else %>
    <%= (company[:zip] + ‘-’ + company[:zip4]) %>
    <% end %>

    <%= company[:country] %>

    rubynuby wrote:

    pardon me, but what’s the significance of -%> instead of the usual %>

    On Nov 15, 6:26 am, Thorsten M. [email protected]

    there’s a rule somewhere: use a dash to remove a line break after the
    ruby code

    It’s in one of my books somewhere…

    Denise R. wrote:

    rubynuby wrote:

    pardon me, but what’s the significance of -%> instead of the usual %>

    On Nov 15, 6:26 am, Thorsten M. [email protected]

    there’s a rule somewhere: use a dash to remove a line break after the
    ruby code

    It’s in one of my books somewhere…

    Found it! Page 41 of Agile Web D. with Rails says:

    "Normally, this doesn’t matter, because HTML doesn’t much care about
    whitespace. However, if you’re using this templating mechanism to
    create emails, or HTML within

     blocks, you’ll want to remove these
    blank lines. Do this by changing the end of the ERb sequence to %> to
    -%>. That minus sign tells Rails to remove any newline that follows
    from that output.

    In general, suppressing these newlines is a matter of taste, not
    necessity. However, you will see Rails code out in the wild that uses
    the minus sign this way, so it’s best to know what it does."

    Since, I was have line break issues, I tried including the minus sign.

    pardon me, but what’s the significance of -%> instead of the usual %>

    On Nov 15, 6:26 am, Thorsten M. [email protected]