how to set default value of form_for select

前端 未结 2 1525
花落未央
花落未央 2021-01-12 20:17

I would like to know how to set default value on form_for select.

My code goes like this:

<%= form_for(@user) do |f| %>
 .
 .
 .

 
2条回答
  •  执念已碎
    2021-01-12 20:53

    You can pass a second option to options_for_select that indicates the selected value.

    options_for_select(@user_groups.collect { |p| [p.name, p.id] }, @user.user_group)
    

    Obviously I'm not sure how your models are set up, but if necessary you use a method like find to locate the entry you want.

提交回复
热议问题