0

Ruby on Rails 3:我有一个表,其中一列根据 objects 布尔属性显示 true 或 false。我希望它显示是或否。

这是我的桌子:

<table id="resellers" class="table table-striped table-hover table-bordered" style="background-color: white">
<thead>
    <button type="button" class="reset">Reset Search</button>
    <th width="12%" onMouseOver="this.style.color='#00F'" onMouseOut="this.style.color='black'">Contact Name</th>
    <th width="12%" onMouseOver="this.style.color='#00F'" onMouseOut="this.style.color='black'">Reseller</th>
    <th width="11%" onMouseOver="this.style.color='#00F'" onMouseOut="this.style.color='black'">Region</th>
    <th width="10%" onMouseOver="this.style.color='#00F'" onMouseOut="this.style.color='black'">Country</th>
    <th width="18%" onMouseOver="this.style.color='#00F'" onMouseOut="this.style.color='black'">Email</th>
    <th width="11%" onMouseOver="this.style.color='#00F'" onMouseOut="this.style.color='black'">Distributor</th>
    <th width="11%" onMouseOver="this.style.color='#00F'" onMouseOut="this.style.color='black'">Registered</th>
    <th width="10%" onMouseOver="this.style.color='#00F'" onMouseOut="this.style.color='black'">Approved</th>
    <th width="5%" class="filter-false"></th>
</thead>
<tbody>
    <% #current_grandstreamer.resellers.each do |reseller| %>
    <% @resellers.each do |reseller| %>

            <tr>
                <td><%= reseller.contact_name %></td>
                <td><%= link_to reseller.company_name, grandstreamers_reseller_path(reseller) %></td>
                <td><%= reseller.company_region %></td>
                <td><%= reseller.company_country %></td>
                <td><%= reseller.contact_email %></td>
                <td><%= reseller.distributor %></td>
                <td><%= reseller.created_at.to_date.to_s %></td>
                <td><%= reseller.approved %></td>
                <td><%= link_to "Edit", edit_grandstreamers_reseller_path(reseller) %>
            </tr>

    <% end %>
</tbody>

有没有办法做一个 if 语句或在控制器中创建一个变量来显示?谢谢

4

2 回答 2

1

像这样的东西会起作用:

<%= reseller.approved ? "Yes" : "No" %>
于 2013-11-08T21:53:31.833 回答
0

我喜欢做的是使用 to_yesno 助手扩展布尔类。

config/initializers/ruby_extensions.rb:

class TrueClass
  def to_yesno
    'yes'
  end
end

class FalseClass
  def to_yesno
    'no'
  end
end

然后在我看来,对于我可以做的任何布尔值:

<%= reseller.approved.to_yesno %>
于 2013-11-08T22:09:51.357 回答