ruby - Rails - Is it hacky to manually list my form options as strings within the select_tag helper? -
is hacky manually list form options strings within select_tag helper?
also, i'm wondering--how use selected value update database?
<div class="form-group"> <%= f.label :sex %><br /> <%= select_tag(:sex_id, options_for_select( [ ["not known", 0], ["male", 1], ["female", 2], ["not applicable", 3] ] )) %>
this question outlines process of using enum
in activerecord (rails 4.1+) mapping integers.
an attribute of type integer
in database can declared in corresponding model (i assume user
):
enum gender: [:not_known, :male, :female, :not_applicable]
in view, may rely on fact system wants corresponding strings, not integers:
<%= f.input :gender, as: :select, collection: user.genders.keys %>
or map them generate want: "human-readable names" or i18n keys:
<%= f.input :gender, as: :select, collection: user.genders.keys.map { |w| [w.humanize, w] } %> <%= f.input :gender, as: :select, collection: user.genders.keys.map { |w| [(t ".w"), w] } %>
Comments
Post a Comment