Skip to content

Instantly share code, notes, and snippets.

@Ch0k
Forked from sameera207/select_tag rails
Created September 15, 2022 12:40
Show Gist options
  • Save Ch0k/5714d75d5f4a2cc7b8e4d1d773e311c6 to your computer and use it in GitHub Desktop.
Save Ch0k/5714d75d5f4a2cc7b8e4d1d773e311c6 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment