set value of simple_form select dropdown with has more than one option with the same value

StackOverflow https://stackoverflow.com/questions/23417805

  •  13-07-2023
  •  | 
  •  

문제

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