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
  •  | 
  •  

Domanda

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

È stato utile?

Soluzione 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.

Altri suggerimenti

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"
            }) %>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top