f.select with enum in rails 3

286 views Asked by At

I have model,

class Test < ActiveRecord::Base
  as_enum :test, [:test, :test_1, :test_1_2]
end

I need to create a f.select dropdown with enum. But I am facing an issue preparing dropdown with enum.

Here is my code:

<%= f.select :test, options_for_select(Test.tests.keys.to_a), {}, :class => "form-control" %>

But prepared a wrong select box. enter image description here

http://grab.by/HSM2

Can anyone have any suggestion?

1

There are 1 answers

0
TarunJadhwani On

Use the code below. Titleize function capitalizes all the words and replaces the underscores with spaces. You can read more about it here

<%= f.select :test, Test.tests.keys.map {|test| [test.to_s.titleize, test]}, {}, :class => "form-control" %>