My tag:
<%= select_tag(:option, options_for_select([['All', 1], ['Co', 2], ['Bought', 3], ['View', 4], ['Top API', 5], :selected => :option ])) %>
How do I set the selected value to which option is selected. For example, if I select ['Bought', 3]
, and submit, ['All', 1]
option is selected. How can I display the selected value after the form is submitted.
You did everything right, just close the options ]
before the :selected => :option
:
instead of ...], selected: :option ]))
, change to ...]], selected: :option ))
So, your code should be:
<%= select_tag(:option, options_for_select([['All', 1], ['Co', 2], ['Bought', 3], ['View', 4], ['Top API', 5]], selected: :option )) %>
Try this:
<%= select_tag(:option, options_for_select([['All', 1], ['Co', 2], ['Bought', 3], ['View', 4], ['Top API', 5]], :selected => params[:option])) %>
This works perfectly in rails 4.2
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