Written September 5, 2013. Tagged Ruby on Rails, Ruby, simple_form.
This is the pattern I've used lately to enumerate values for simple_form select
options:
Values:
class Order
class Status
KEYS = [
NEW = "new",
INVOICED = "invoiced",
PAID = "paid",
SHIPPED = "shipped"
]
def self.keys
KEYS
end
def self.all
keys.map { |key| new(key) }
end
def initialize(key)
@key = key
end
# simple_form automatically uses `id` for the option value.
def id
@key
end
# simple_form automatically uses this for the option text.
def name
I18n.t(@key, scope: :"models.order.statuses")
end
end
end
View:
…
<%= f.input :status, collection: Order::Status.all %>
…
And you can validate like this:
class Order < ActiveRecord::Base
validates :status, inclusion: { in: Status.keys }
end
If you have something better, let me know!