how to set default value of form_for select

前端 未结 2 1526
花落未央
花落未央 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:51

    Add a parameter :selected =>

        <%= form_for(@user) do |f| %>
         .
         .
         .
    
         
    <%= f.select(:user_group_id, options_for_select(@user_groups.collect {|p| [ p.name, p.id ] }, "Select Category"), :selected =>f.object.user_group_id) %>
    <%= end %>

提交回复
热议问题