Reputation: 12326
I have a model question which has a column called category.
I have an array that lists all valid categories: Question.categories
<%= form_for(@question) do |f| %>
<%= f.select :category, options_for_select(Question.categories) %>
#...
<% end %>
Say I have a variable called @currentlySelectedCategory.
Now how do I tell rails to preselect the option in the dropdown menu that matches @currentlySelectedCategory?
Upvotes: 7
Views: 3284
Reputation: 21884
<%= form_for(@question) do |f| %>
<%= f.select :category, options_for_select(Question.categories, @currentlySelectedCategory) %>
#...
<% end %>
But since you are using form_for
, I would have thought that rails would select the question category.
Upvotes: 7