customer_gender
是一个数组数组。
我想在编辑表单上选择当前值 但它没有用。
我认为问题是性别的价值不是数字而是字符串,
如果值是字符串
,如何解决此问题customer_gender= [
['男', male],
['女', female]
]
= simple_form_for(@customer) do |f|
.form-inputs
- customer_gender = Customer::GENDER.collect do |gender|
- t(gender)
= f.input :gender,label: t("gender"),as: :select, |
:collection => customer_gender, |
:include_blank => false, :selected => @customer.gender
irb(main):003:0> cols(Customer)
[
[ 0] "id", ...
[11] "gender",
]
如果我必须在gender
列中添加数字,
我如何应用i18n转换1->男性,2岁>女性?
答案 0 :(得分:0)
您无法使用.to_i
方法将.gender
值转换为整数吗?
= simple_form_for(@customer) do |f|
.form-inputs
= f.input :gender, label: t("gender"), as: :select, |
:collection => @genders, |
:include_blank => false, :selected => @customer.gender.to_i
答案 1 :(得分:0)
添加到模型
def gender_txt
["Not Telling", "Male", "Female"][self.gender - 1]
end
and in view
<%= f.select :gender, User::GENDER_TYPES, 'data-bind' => 'value: user.gender', selected: @user.gender_txt %>
这将显示整数性别列的相应文本。