5

Is it possible to somehow use the :selected option that you'd use on a normal select view helper with the grouped_collection_select function? I'd like to set the value that gets pre-selected in my list. I've tried passing in :selected as an option with no luck!

Here's some code snippts of my tests:

grouped_collection_select 'user[subscription_attributes]', :subscription_plan_id, Trade.order(:name).all, :subscription_plans, :name, :id, :display_name, { :include_blank => true, :selected => 5 }

grouped_collection_select 'user[subscription_attributes]', :subscription_plan_id, Trade.order(:name).all, :subscription_plans, :name, :id, :display_name, :include_blank => true, :selected => 5 

Neither version works. No selected is set. I'm using this to set a value for a nested model. I'm using the railscasts dynamic select list methods: http://railscasts.com/episodes/88-dynamic-select-menus-revised

I couldn't get formtastic to play nicely with the group selects so I had to do it by hand but I don't keep this value selected when a user fails validations. I'd like to keep this set when they fix validation errors.

2
  • Seems to be a bug/feature not yet added to Rails for grouped_collection_select. Apparently theres a fix for it on master branch only. I think the only option would be to use Javascript.
    – Noz
    Sep 27, 2012 at 21:50
  • #barancw Did you ever figure out how to pre-select for a grouped_collection_select? Reddirt
    – Reddirt
    Sep 18, 2013 at 20:43

3 Answers 3

9

I just ran across the same problem and solved it using the option_groups_from_collection_for_select helper and the select helper documented at: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html.

The first step is to create the grouped options. Taking your example, it should look like:

<% options = option_groups_from_collection_for_select(Trade.order(:name).all,
   :subscription_plans, :name, :id, :display_name, 5) %>

Then I created the select object like:

<%= select('user[subscription_attributes]', :subscription_plan_id, options, 
  include_blank: true) %>

You could write it all out in one line, I just broke out the options into a separate variable to illustrate the two different methods.

1

Maybe too late, but the API documentation of grouped_collection_select states: 'The value returned from calling method on the instance object will be selected.'

So, you don't even have to specify a :selected option, since Rails will automatically select based on the current value of your attribute. If subscription_plan_id has value 5, then that's what will be selected.

If that's supposed to be a default value, then you can set it with an after_initialize in your model.

0

Actually you need to send in options include_blank for example

<%= grouped_collection_select :id, model.all, options = {:include_blank => 'Selecione'}%>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.