有没有办法将select转换为此语句的select_tag:
<%= select "invoice_product_code",
@invoice_product_array.each_with_index.map {
|jsondata, index|
[jsondata["invoice_product_code"],
index]},
{:selected => item["invoice_product_code"]},
:style => "width:110px"
%>
记住,
我想从数组中定义每个选项值和选项文本
选择的选项
式
就像上面那样。
这是生成的HTML:
<select id="invoice_product_code_invoice_product_code" name="invoice_product_code[invoice_product_code]" style="width:110px"><option value="0">INVPRODA</option>
<option value="1">INVPRODB</option>
<option selected="selected" value="2">INVPRODC</option>
<option value="3">INVPRODD</option>
<option value="4">INVPRODE</option>
<option value="5">INVPRODF</option>
<option value="6">INVPRODG</option></select>
我有一种不可能的感觉。无法从API http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag
中找到合适的示例这不起作用:
<%= select_tag "invoice_product_code",
@invoice_product_array.each_with_index.map {
|jsondata, index|
[jsondata["invoice_product_code"],
index]}
%>
这不起作用: 它是正确的,因为值和显示文本是一个json字符串而不是数组项中的特定键值。
<%= select_tag "invoice_product_code",
options_for_select(@invoice_product_array)
%>
答案 0 :(得分:0)
options_for_select
需要额外的参数来指定所选的值,每个选项都可以将HTML属性作为最后一个元素。
<%= select_tag "invoice_product_code", options_for_select(
@invoice_product_array.each_with_index.map {
|jsondata, index|
[jsondata["invoice_product_code"],
index,
style: 'width: 110px;']},
item["invoice_product_code"])
%>