我的代码目前看起来像这样:
<%= f.input(:l_duty,
:required => true,
:label => 'Weight Class',
:collection => {"GVWR of 8,500 pounds or less" => true, "GVWR of more than 8,500 pounds" => false, "Neighborhood Electric Vehicle" => true},
:input_html => {
:value => :collection[@vehicle.category], #?????????
:class => "span10"
}) %>
我需要能够根据:value
中的密钥选择:collection
,而不是值。当@vehicle.l_duty
设置为true时,Neighborhood Electric Vehicle
始终选择@vehicle.category
。我还有:collection
与{{1}}哈希
答案 0 :(得分:0)
从问题中不清楚,但在我看来,你不需要哈希的价值。在这种情况下,只需传递一个数组而不是hash:
<%= f.input(:l_duty,
:required => true,
:label => 'Weight Class',
:collection => ["GVWR of 8,500 pounds or less", "GVWR of more than 8,500 pounds", "Neighborhood Electric Vehicle"],
:input_html => {
:value => @vehicle.l_duty,
:class => "span10"
}) %>
答案 1 :(得分:0)
我最终用javascript选择了正确的字段:
if ($('#standard_vehicle_l_duty :selected').html() == "Neighborhood Electric Vehicle" && "<%= @vehicle.category %>" != "NEV") {
$('#standard_vehicle_l_duty option:nth-child(2)').prop('selected', true);
}
不是很优雅,但它有效。