dgreen22
dgreen22

Reputation: 398

Eliminate Empty lines when present? method used

I have a product show page with attributes, but not all are mandatory. So I'm using the present? method to determine if the attribute is present, and if it's blank, it doesn't show any field label or input.

This works fine but my problem is it leaves blank lines instead of condensing. So for example the user sees this on the product page:

Air conditioning: Window Unit 

Flooring: Carpet

Gets created if the user doesn't input anything for basement (notice gap between two lines - if an attributes is blank, I want the gap eliminated and next line presented).

Here's my product/show:

<% if @property.air_conditioning.present? %>
  <%= label_tag :air_conditioning %>: 
  <%= @property.air_conditioning %>
<% end %><br />
<% if @property.basement.present? %>
  <%= label_tag :basement %>: 
  <%= @property.basement %>
<% end %><br />
<% if @property.flooring.present? %>
  <%= label_tag :flooring %>: 
  <%= @property.flooring %>
<% end %><br />

How do I stop this?

Upvotes: 0

Views: 20

Answers (1)

davegson
davegson

Reputation: 8331

If you look at your if statement closely, you will notice that the <br/> tag is outside the if-block. So it will insert breaks even if there is no products, leading to the new lines.

So change it to this:

<% if @property.air_conditioning.present? %>
  <%= label_tag :air_conditioning %>: 
  <%= @property.air_conditioning %>
  <br/>
<% end %>
<% if @property.basement.present? %>
  <%= label_tag :basement %>: 
  <%= @property.basement %>
  <br/>
<% end %>
<% if @property.flooring.present? %>
  <%= label_tag :flooring %>: 
  <%= @property.flooring %>
  <br/>
<% end %>

Upvotes: 1

Related Questions