forked from railsadminteam/rails_admin
-
Notifications
You must be signed in to change notification settings - Fork 1
Enumeration
clancour-taleo edited this page Apr 13, 2012
·
11 revisions
The :enum
field type is for when you need to display a list of potential values. It will be rendered with a select box in forms.
Other advantage, a filter with a select box will be added too.
As usual with RailsAdmin, there are two ways to do this.
If you have a :color
column in your Team model, RailsAdmin will check if Team#color_enum exists.
If it does, then you're done.
The result call will be sent to FormOptionsHelper#options_for_select
to fill the select box.
See this for possible output (hash, array)
def Team < ActiveRecord::Base
def color_enum
# Do not select any value, or add any blank field. RailsAdmin will do it for you.
['green', 'white']
end
end
# you need to tell RailsAdmin that you want to use an `:enum` field
field :color, :enum do
# if your model has a method that sends back the options:
enum_method do
:my_color_enum_instance_method
end
# or doing it directly inline
enum do
['green', 'white']
end
end
During Create/Update, display a Multi-Select List box for :roles field. Stores/Retrieves the selected options as array into a single db string field as serialized array.
edit do
field :roles do
render do
bindings[:form].select( "roles", bindings[:object].roles_enum, {}, { :multiple => true })
end
end
end
class User < ActiveRecord::Base
serialize :roles, Array
def roles_enum
[ [ 'role one', 1 ], [ 'role 2', 2 ], [ 'role 3', 3 ] ]
end
end