自定义布尔文本?

时间:2011-11-23 21:12:18

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

如果在我的BusinessStore模型/表中,我有布尔值:

create_table :business_stores do |t|
    t.boolean :online_store
end

在我看来,我希望它说“在线”而不是真或假为字符串:

<% @business_stores.each do |business_store| %>
    <%= business_store.online_store %>
<% end %>

怎么做?

3 个答案:

答案 0 :(得分:5)

我遵循规则来保持逻辑不在视图中,因此我将在BusinessStore模型中创建一个方法:

def BusinessStore < ActiveRecord::Base
    def status
       if online_store
         "Online"
       else
         "Some other type or blank"
       end
    end
end

然后在视图中

<%= business_store.status %>

答案 1 :(得分:4)

也许是这样:

<%= business_store.online_store ? "Online" : "Offline" %>

答案 2 :(得分:3)

<% @business_stores.each do |business_store| %>
    <%= "Online" if business_store.online_store %>
<% end %>