简体   繁体   中英

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 ie 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!

您需要将键传递给集合,而不是枚举。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM