Rails Form Won't Select Default Value
Rails 4.0 Ruby 2.0, SimpleForm 3.1.0.rc2 Why isn't the default value selected in the first example when It is selected in the second example? Both examples are in the same form.
Solution 1:
Because the location variable is polymorphic, there is no "belongs to" association from car. Therefore, value will not be set. To get this to happen, I developed a JQuery that fires on ready to grab the value, which is actually the ID of the select, and set the value, as follows:
<%= f.input(:location_id, {input_html: {value: @car.location_id}, collection: Location.all.order("name").collect { |c| [c.name, c.id] }, prompt: "Location?"}) %>
$("select[name='car[location_id]']").ready(function () {
// Obtain the value attribute and set val parameter
var location = $('#car_location_id').attr("value");
$("#car_location_id").val(location);
});
Kudos to janfoeh for leading me to this. OBTW, I tried this all using the location variable but the select needed the ID.
Post a Comment for "Rails Form Won't Select Default Value"