Ruby on rails 自定义布尔文本?
如果在我的BusinessStore模型/表中有布尔值:Ruby on rails 自定义布尔文本?,ruby-on-rails,ruby,ruby-on-rails-3,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 %> 怎么做
create_table :business_stores do |t|
t.boolean :online_store
end
在我看来,我希望它说“在线”,而不是像字符串一样的真或假:
<% @business_stores.each do |business_store| %>
<%= business_store.online_store %>
<% end %>
怎么做呢?也许是这样:
<%= business_store.online_store ? "Online" : "Offline" %>
?我按照规则将逻辑排除在视图之外,因此我将在BusinessStore模型中创建一个方法:
def BusinessStore < ActiveRecord::Base
def status
if online_store
"Online"
else
"Some other type or blank"
end
end
end
def BusinessStore
然后在视图中
<%= business_store.status %>
<%= business_store.status %>