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