1

I have a category of items lets say :

  • Sold
  • In shop
  • Coming soon

Every items are record in database as follow :

  • item.category = 0 for sold
  • item.category = 1 for shop
  • item.category = 2 for coming soon

Today I do as follow :

<% if item.category = 0 %>Sold<% end %>
<% if item.category = 1 %>in shop<% end %>
<% if item.category = 2 %>coming soon<% end %>

Is there a way to centralize an enum for all my application so it will be available in any pages like this :

<% if SOLD %>Sold<% end %>
ZazOufUmI
  • 3,212
  • 6
  • 37
  • 67

1 Answers1

1

You could put the enum in a helper class such as application_helper.rb under the helpers folder in the app folder.

I think beginning with rails 3.2? all helpers are available in all controllers and views

for example adding the following to your application_helper.rb file should allow you to use

get_category(0)

in any view to return "Sold"

  CATEGORY = {
    0 => "Sold",
    1 => "in Shop",
    2 => "comming soon"

  }.freeze

  def get_category(arg)
    CATEGORY[arg]
  end
nPn
  • 16,254
  • 9
  • 35
  • 58