我正在从多个复选框中获取值
<%for med_for in ['Self','Father', 'Mother', 'Spouse', 'Child_1', 'Child_2']%>
<label style="display: inline-block;margin: 6px;"> <%=f.check_box :mediclaim_for,{ multiple: true }, :value=>med_for %>
<%= med_for %> </label>
<%end%>
我有一个像这样的数组:
[
"0", "{:value=>\"Self\"}",
"0", "{:value=>\"Father\"}",
"0", "{:value=>\"Mother\"}",
"0", "{:value=>\"Spouse\"}",
"0", "0"
]
在控制器中
cand_dtl=params[:my_detail]
medi_for_array = cand_dtl[:mediclaim_for].inspect //getting above array
abort medi_for_array.map{|s| s[/\{:value=>\"(.*)\"\}/, 1]}.compact
我怎么只能得到像这样的值:
"Self", "Father", "Mother", "Spouse"
答案 0 :(得分:2)
a = [
"0", "{:value=>\"Self\"}", "0", "{:value=>\"Father\"}",
"0", "{:value=>\"Mother\"}",
"0", "{:value=>\"Spouse\"}", "0", "0"
]
a.map{|s| s[/\{:value=>\"(.*)\"\}/, 1]}.compact
# => ["Self", "Father", "Mother", "Spouse"]
答案 1 :(得分:1)
arr.reject { |s| s == '0' }.map { |r| eval(r)[:value] }