Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preselect an option with rails f.select helper?

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?

like image 591
Razor Storm Avatar asked Dec 30 '11 22:12

Razor Storm


1 Answers

<%= 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.

like image 195
Robin Avatar answered Nov 04 '22 10:11

Robin