Skip to content

Instantly share code, notes, and snippets.

@sameera207
Created March 22, 2012 18:51
Show Gist options
  • Save sameera207/2161748 to your computer and use it in GitHub Desktop.
Save sameera207/2161748 to your computer and use it in GitHub Desktop.
Using selected option in rails select_tag
year array -> ['2012','2011','2010','2009']
selected year -> 2010
<%= select_tag 'year_range', options_for_select(['2012','2011','2010','2009'],'2010') %>
which will always selects '2010'
If you are selecting an object
if the form is -> f
collection is @ages
selected index is always 3
<%= f.select :age, options_from_collection_for_select(@ages, "id", "age",'3') %>
resources
http://stackoverflow.com/questions/7347039/rails-select-tag-selected-value
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select
@wellington1993
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment