0

以下适用于在下拉列表中选择当前用户的主要工作组:

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

但是,你如何:selected为 grouped_collection_select 设置一个?

代码尝试:

        <%= f.grouped_collection_select :employee_id, Workgroup.order(:id), :employees, :group_name, :id, :employee_full_name, :selected => current_user.employee.id %>

        <%= f.grouped_collection_select :employee_id, Workgroup.order(:id), :employees, :group_name, :id, :employee_full_name, :include_blank => true, :selected => [current_user.employee.priworkgroup_id, current_user.employee.id] %>

更新1

阅读有关 grouped_collection_select 的信息,我发现 selected_key

我试过这个,但没有奏效:

<%= 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 %>

current_user.employee.id是 = 5

检查 html,我想要选择的用户有这个:

<option value="5">John Jones</option>

更新 - 感谢 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) %>

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

我改成这样:

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

并得到undefined method合并'`

4

1 回答 1

0

这有效:

<% 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) %>
于 2013-09-21T21:05:18.240 回答