I want to display my dropdown form selected option value whenever an user edits the form. Right now it displays the first option (in this case a blank option).
Form
<%= f.select(:condition, options_for_select([["Brand New", "Brand New"], ["Pre-owned", "Pre-owned"]]), :include_blank => true, :selected => params[:condition]) %>
HTML Output
<select id="product_condition" name="product[condition]">
<option value=""></option>
<option value="Brand New">Brand New</option>
<option value="Pre-owned">Pre-owned</option>
</select>
JSON
{
id: 2,
condition: "Pre-owned",
}
Thanks.
<%= f.select(:condition, options_for_select([["Brand New", "Brand New"], ["Pre-owned", "Pre-owned"]], :selected => f.object.condition), :include_blank => true) %>
Check the options_for_select
documentation, and you will discover that the last parameter is the selected option.
options_for_select(container, selected = nil)
In your case
<%= f.select(:condition, options_for_select([["Brand New", "Brand New"], ["Pre-owned", "Pre-owned"]], params[:condition]), :include_blank => true) %>
assuming params[:condition]
contains the currently selected value, and the value matches the corresponding value in the select tag.
In other words, for "Pre-owned"
to be selected, params[:condition]
must contain "Pre-owned"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With