0

我想将三个变量添加到同一个表格单元格中,并用 / 分隔它们,因此 BR/BA 列将有一个这样的条目:2/2/0,下面是我的微弱尝试,它当然会返回语法错误,怎么办我做吗?谢谢,亚当

<table class="listing" summary="Property list">
<tr class="header">

  <th>Property Address</th>
  <th>Price</th>
  <th>Sq Ft</th>
  <th>BR/BA</th>
  <th>Type</th>
</tr>
<% @properties.each do |property| %>
<tr>

  <td><%= link_to(property.address, {:action => 'show', :id => property.id}) %></td>
  <td class="center"><%= property.price %></td>
  <td class="center"><%= property.sq_ft %></td>
  <td class="center"><%= {property.bedrooms "/" property.bathrooms "/" property.half_bathrooms} %></td>
  <td class="center"><%= property.property_type %></td>
</tr>
<% end %>

4

3 回答 3

1

[property.bedrooms,property.bathrooms,property.half_bathrooms].join("/")

假设每个属性/方法返回一个字符串。

于 2012-05-17T18:24:33.680 回答
1

在 Ruby 中连接字符串是用+.

property.bedrooms + "/" + property.bathrooms + "/" + property.half_bathrooms

此外,一般来说,每当您在“我有语法错误”的问题中发布时,如果您发布了这些语法错误是什么,将会很有帮助。

于 2012-05-17T18:25:40.643 回答
1

您可以添加 3 个字段

attr_accessor :bedrooms, :bathrooms, :half_bathrooms

before_save :combine_cols

def combine_cols
    self.brba = "#{bedrooms}/#{bathrooms}/#{half_bathrooms}"
end

def brba=(val)
    bedrooms, bathrooms, half_bathrooms = val.split('/')
end

这只是我的想法,所以可能无法完全按原样工作。

于 2012-05-17T18:30:10.187 回答