如何将对象的属性显示为不同的字符串?

时间:2013-11-08 21:51:55

标签: html ruby-on-rails ruby ruby-on-rails-3

Ruby on Rails 3:我有一个表,其中一列显示基于对象boolean属性的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语句或变量? 感谢

2 个答案:

答案 0 :(得分:1)

这样的事情会起作用:

<%= reseller.approved ? "Yes" : "No" %>

答案 1 :(得分: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 %>