Preselect option with rails f.select helper?

I have a model question that has a column called a category.

I have an array listing all valid categories: Question.categories

<%= form_for(@question) do |f| %> <%= f.select :category, options_for_select(Question.categories) %> #... <% end %> 

Say I have a variable @currentlySelectedCategory.

Now, how can I specify rails to pre-select an option from the drop-down menu that matches @currentlySelectedCategory?

+6
source share
1 answer
 <%= 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 the rails would choose a category of questions.

+7
source

Source: https://habr.com/ru/post/904776/


All Articles