how to set default value of form_for select

雨燕双飞 提交于 2019-12-01 05:41:31

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.

Add a parameter :selected =>

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

     <div class="field">
     <%= f.select(:user_group_id, options_for_select(@user_groups.collect {|p| [ p.name, p.id ] },   "Select Category"), :selected =>f.object.user_group_id) %>
     </div>
    <%= end %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!