当存在时消除空行?使用的方法

时间:2016-03-10 16:25:58

标签: html ruby-on-rails

我有一个带有属性的产品展示页面,但并非所有都是强制性的。所以我使用present?方法来确定属性是否存在,如果它是空白的,则它不会显示任何字段标签或输入。

这很好但我的问题是留下空白而不是冷凝。例如,用户在产品页面上看到了这一点:

Air conditioning: Window Unit 

Flooring: Carpet

如果用户没有为地下室输入任何内容,则会创建(注意两行之间的间隙 - 如果属性为空,我希望消除间隙并显示下一行)。

这是我的产品/展示:

<% 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 />

如何阻止此事?

1 个答案:

答案 0 :(得分:1)

如果仔细查看if声明,您会注意到<br/>标记之外的if-block。因此即使没有产品也会插入中断,从而产生新的线条。

所以改成它:

<% 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 %>