Ruby on rails Rails\u集合:已选定

Ruby on rails Rails\u集合:已选定,ruby-on-rails,grouped-collection-select,Ruby On Rails,Grouped Collection Select,在下拉列表中选择当前用户的主要工作组时,可以使用以下方法: <%= f.association :workgroup, :collection => Workgroup.all, :label_method => :group_name, :label => '1) Work Group', :selected => current_user.employee.priworkgroup_id %> 更新1 阅读有关分组的\u集合\u选择,我找

在下拉列表中选择当前用户的主要工作组时,可以使用以下方法:

        <%= f.association :workgroup, :collection => Workgroup.all, :label_method => :group_name, :label => '1) Work Group', :selected => current_user.employee.priworkgroup_id %>
更新1

阅读有关分组的\u集合\u选择,我找到了选中的\u键

我试过这个,但没用:

<%= f.grouped_collection_select :employee_id, Workgroup.order(:id), :employees, :group_name, :id, :employee_full_name, :include_blank => true, :selected_key => current_user.employee.id %>
更新-感谢Fenec

我使用了以下方法:

<% options = option_groups_from_collection_for_select(Workgroup.order(:id),
       :employees, :group_name, :id, :employee_full_name, current_user.employee.id) %>
<%= select(:employee_id, :employees, options, include_blank: true) %>

并且它在屏幕上正确显示。但是,select不会更新记录中的数据

我改为:

<%= f.select(:employee_id, :employees, options, include_blank: true) %>

并得到了
未定义的方法
merge'`

这是有效的:

<% options = option_groups_from_collection_for_select(Workgroup.order(:id),
       :employees, :group_name, :id, :employee_full_name, current_user.employee.id) %>

<%= f.select(:employee_id,  options, include_blank: true) %>


尝试使用此解决方案:#Fenec-这很有效!对我来说似乎很困惑-但是,嘿,它是有效的。谢谢如果你想要分数,我愿意给你。雷迪特,太好了。当Rails助手有这么多选项要设置时,他们常常会感到困惑。你最好把你的观点保存下来,以便得到真正的答案,我刚刚发布了一个链接。#Fenec-我遇到了一个问题=屏幕上正确显示了
select
,但它不是表单输入。我更改了它
f.select
,得到了未定义的方法“merge”。我应该将select更改为什么?
<%= f.select(:employee_id, :employees, options, include_blank: true) %>
<% options = option_groups_from_collection_for_select(Workgroup.order(:id),
       :employees, :group_name, :id, :employee_full_name, current_user.employee.id) %>

<%= f.select(:employee_id,  options, include_blank: true) %>