kofhearts
kofhearts

Reputation: 3774

how do you make enums work with simple_form?

Please consider the following model

  class Song < ActiveRecord::Base

      enum category: [:english, :french]

      enum file_type: [:mp3, :video]

      enum mood: [:sad, :happy]
    end

I have a form

= simple_form_for(@song) do |f|

  = f.input :name
  = f.input :category, collection: Song.categories
  = f.input :file_type, collection: Song.file_types
  = f.input :mood, collection: Song.moods

Here is the problem is when i edit the form then the selected value is nil i.e the select box doesnt select the value that was set instead it selects blank. So i am wondering in the view is there a way to show the saved enum value?

Thanks!

Upvotes: 3

Views: 1666

Answers (1)

coderhs
coderhs

Reputation: 4837

You need to pass the keys to the collection instead of the enum.

= f.input :category, collection: Song.categories.keys

Upvotes: 9

Related Questions