你如何让枚举与 simple_form 一起工作?
how do you make enums work with simple_form?
请考虑以下型号
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
问题是当我编辑表单时 selected 值为 nil 即 select 框没有 select 设置的值而是 select 空白。所以我想知道在视图中有没有办法显示保存的枚举值?
谢谢!
您需要将键传递给集合而不是枚举。
= f.input :category, collection: Song.categories.keys
请考虑以下型号
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
问题是当我编辑表单时 selected 值为 nil 即 select 框没有 select 设置的值而是 select 空白。所以我想知道在视图中有没有办法显示保存的枚举值?
谢谢!
您需要将键传递给集合而不是枚举。
= f.input :category, collection: Song.categories.keys