Skip to content

tanakaworld/enum_select_rails

Repository files navigation

EnumSelectRails

Gem Version

A form helper for the enum field of Rails model.

You can easily generate select tag which has enum field options.

image

Installation

Add this line to your application's Gemfile:

gem 'enum_select_rails'

Usage

class User
  enum gender: {    
    female: 2,
    male: 1,
    other: 9
  }
end
<%= f.enum_select :gender %>

i18n

Automatically apply i18n text into options text.

This feature is depends on enum_help.

image

# Gemfile
gem 'enum_help'
# application.rb
# i18n
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]
# config/locales/models/user/ja.yml
ja:
  activerecord:
    models:
      user: ユーザー
    attributes:
      user:
        gender: 性別
  enums:
    user:
      gender:
        female: 女性
        male: 男性
        other: その他

License

The gem is available as open source under the terms of the MIT License.