add selected and disabled options to grouped select #5083

Merged
merged 1 commit into from Feb 20, 2012

Conversation

Projects
None yet
4 participants
Contributor

nashby commented Feb 18, 2012

No description provided.

@josevalim josevalim added a commit that referenced this pull request Feb 20, 2012

@josevalim josevalim Merge pull request #5083 from nashby/grouped-select-options
add selected and disabled options to grouped select
b62f5a9

@josevalim josevalim merged commit b62f5a9 into rails:master Feb 20, 2012

Setting the selected option always makes the group select default to the first available option for me given the following code for simple_form:

= f.input :state_or_province, :collection => Country.order(:name), :as => :grouped_select, :group_method => :states, :selected => params[:passed][:school][:state_or_province]
Owner

rafaelfranca commented Sep 27, 2012

@12eebok As far I remember this fix is only in the master branch.

@rafaelfranca You sure? Setting disabled seems to work just fine for me, I'm just having trouble with the selected option.

Owner

rafaelfranca commented Sep 27, 2012

I'm not sure, maybe this is a SimpleForm issue. have you tried the plain grouped_collection_select?

The form_for equivalent:

f.grouped_collection_select :state_or_province, Country.order(:name), :states, :name, :id, :name, {:selected => params[:passed][:school][:state_or_province]}, {:disabled => true}

Produces exactly identical output with my default option being Zabul(first option in the collection) in both cases:

<select name="school[state_or_province]" id="school_state_or_province" class="grouped_select optional">
 ....
   <optgroup label="Afghanistan">
      <option value="15">Zabul</option>
       ...

Expected output given a key value of 448:

<optgroup label="Canada">
      <option selected="selected" value="448">Manitoba</option>
  ...

Unless I'm not passing the selected option correctly I don't think it's a simple_form issue.

Owner

rafaelfranca commented Sep 27, 2012

Yes, this fixes is only to master.

Owner

rafaelfranca commented Sep 27, 2012

Here the relevant code https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/action_view/helpers/form_options_helper.rb#L606 see the last argument of the option_groups_from_collection_for_select method

Good to know, thank you for your time

Owner

rafaelfranca commented Sep 27, 2012

If you want you can make a pull request with the tests in this one and you implementation (since the master branch and the 3-2-stable are very different in these helpers).

I could make it, but I don't have time now

I'd be willing to give it a shot, but I'm still a little green to Github and even Rails for that matter so I'll have to do a little reading first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment