Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom boolean text?

If in my BusinessStore model/table I have the boolean:

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

And in my view I wanted it to say "Online" instead of true or false as a string:

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

How would it be done?

like image 309
LearningRoR Avatar asked Aug 21 '26 14:08

LearningRoR


2 Answers

I go by the rule to keep the logic out of views, so I would create a method in the BusinessStore model:

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

Then in the view

<%= business_store.status %>
like image 188
natedavisolds Avatar answered Aug 23 '26 05:08

natedavisolds


Maybe so:

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

?

like image 45
WarHog Avatar answered Aug 23 '26 04:08

WarHog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!