My code currently looks like this:

<%= 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"
                }) %>

And I need to be able to select the :value based on the key in the :collection, not the value. @vehicle.l_duty always selects Neighborhood Electric Vehicle when it's set to true. I have also have @vehicle.category which correlates to the keys in the :collection hash

有帮助吗?

解决方案 2

I ended up selecting the correct field with 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);
}

Not very elegant, but it works.

其他提示

It is not clear from the question, but is seems to me that you don't need the value of you hash. In that case, simply pass an array instead of 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"
            }) %>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top