1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Rails]enumをもっと使いやすく!Enumelizeさん!

Posted at

enumelizeとは、、?

I18n と ActiveRecord/Mongoid/MongoMapper/Sequel のサポートを伴った enum 型の属性。
あるモデルをstatus(ステータス)で分類したい時などに使います。

##設定方法
1.Gemfileに以下を追記する。


gem 'enumerize'

合わせてこちらも実行する。


$ bundle install

2.モデルに以下の記述を追記する。


class User
  extend Enumerize

  enumerize :sex, in: [:male, :female]
end

3.yamlファイルに以下の記述をする。


ja:
  enumerize:
    user:
      sex:
        male: 
        female: 

##使い方

  • 値を出力したい場合

User.sex.values  # => ["male", "female"]

User.sex.options # => [["男", "male"], ["女", "female"]]

user = User.new

user.sex_value # => "male"
user.sex_text # => "男"
  • セレクトボックスに利用したい場合

<%= form_for @user do |f| %>
  <%= f.select :sex, User.sex.options %>
<% end %>

##参考
RailsでEnumerizeの使い方

gem enumerize の README を翻訳しました

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?