0

The following works for having the current user's primary workgroup selected in a dropdown:

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

But, how do you set a :selected for grouped_collection_select ?

Code attempts:

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

UPDATE1

Reading about grouped_collection_select, I found selected_key

I tried this, but it didn't work:

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

Inspecting the html, the user I want selected has this:

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

UPDATE - thanks to Fenec.

I used the following:

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

And it displays correctly on the screen. But, the select doesn't update the data in the record.

I changed to this:

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

And got undefined methodmerge'`

dax
  • 10,779
  • 8
  • 51
  • 86
Reddirt
  • 5,913
  • 9
  • 48
  • 126
  • try using this solution: http://stackoverflow.com/questions/11817496/how-to-use-selected-with-grouped-collection-select – Fenec Sep 20 '13 at 22:15
  • #Fenec - that works! Seems confusing to me - but, hey it works. Thanks! If you want points, I'm willing to give them to you. Reddirt. – Reddirt Sep 20 '13 at 22:39
  • Great. Rails helpers are often confusing when they have so many options to set. You better save your points for some real answer, I just posted a link. – Fenec Sep 20 '13 at 22:45
  • #Fenec - I ran into an issue = the `select` shows on the screen correctly, but it isn't a form input. I changed it `f.select` and got undefined method `merge'. What should I change the select to? – Reddirt Sep 20 '13 at 23:00

1 Answers1

0

This worked:

<% 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) %>
Reddirt
  • 5,913
  • 9
  • 48
  • 126