simple_form选择下拉列表的设置值具有多个具有相同值的选项

时间:2014-05-01 22:34:12

标签: ruby-on-rails ruby simple-form

我的代码目前看起来像这样:

<%= 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}}哈希

中的密钥相关联

2 个答案:

答案 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);
}

不是很优雅,但它有效。