你如何让enum与simple_form一起工作?

问题描述 投票:1回答:1

请考虑以下型号

  class Song < ActiveRecord::Base

      enum category: [:english, :french]

      enum file_type: [:mp3, :video]

      enum mood: [:sad, :happy]
    end

我有一张表格

= 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

问题是,当我编辑表单时,所选值为nil,即选择框没有选择设置的值而是选择空白。所以我想在视图中有没有办法显示保存的枚举值?

谢谢!

ruby-on-rails ruby enums simple-form
1个回答
3
投票

您需要将密钥传递给集合而不是枚举。

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

© www.soinside.com 2019 - 2024. All rights reserved.