Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails select tag selected value

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.

like image 862
Yogzzz Avatar asked Mar 06 '13 16:03

Yogzzz


2 Answers

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 )) %>
like image 57
gabrielhilal Avatar answered Sep 26 '22 08:09

gabrielhilal


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

like image 36
Jonathan MacDonald Avatar answered Sep 22 '22 08:09

Jonathan MacDonald