I tried using enum instead of Active Hash in application development, so I summarized it lightly.
This time we will use enum for the column called gender. When using enum, the column type must be ** integer type **.
Column name | Column type |
---|---|
gender | integer |
Describe the data in the model file. The enum is defined in the form of a hash like the one below.
enum gender: { man: 0, woman: 1, other: 2 }
This time it was displayed as a radio button. Describe the column name in the first argument and the value defined in the model in the second argument.
<div class="field">
<%= f.label :man %>
<%= f.radio_button :gender, :man %>
<%= f.label :woman %>
<%= f.radio_button :gender, :woman %>
<%= f.label :other %>
<%= f.radio_button :gender, :other %>
</div>
Send man as key in view → Controller → Find number based on key sent in model → Save as numerical value in database
https://madogiwa0124.hatenablog.com/entry/2017/12/24/222156
Recommended Posts