Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Make "select" helper handle nested collections #1722

merged 1 commit into from Jul 11, 2011


None yet
5 participants

AndrewRadev commented Jun 15, 2011

The select form helper generates a select tag with options from the given [key, value] pairs. This works only for "flat" collections, but I'd like to have it work with nested ones as well. An example:

@post = Post.new
select("post", "origin", {
  "Africa" => [["South Africa", "sa"], ["Somalia", "so"]],
  "Europe" => [["Denmark", "dk"], ["Ireland", "ie"]],

Currently, select uses "Africa" and "Europe" as the content of the option tags, and calls to_s on the arrays for the values. With this pull request, the result would instead be something like this:

<select id="post_origin" name="post[origin]">
  <optgroup label="Africa">
    <option value="sa">South Africa</option>
    <option value="so">Somalia</option>
  <optgroup label="Europe">
    <option value="dk">Denmark</option>
    <option value="ie">Ireland</option>

Internally, the choices are simply handed to grouped_options_for_select if the collection is more than one level deep.



gmile commented Jun 17, 2011



sikachu commented Jul 11, 2011

This looks great. Do you mind merge and rebased your patch against current master so we can merge it in? We'd really like the test case and implementation into a single commit.

Thank you.


AndrewRadev commented Jul 11, 2011

I rebased the code against the current master and force-pushed, so it should merge cleanly now. I also squashed the first test case and the first implementation attempt together. Is that okay or should I just clump it all in a single commit? Personally, I'd prefer to keep it as it is right now, since the history might be useful, even if it is just a small patch.


sikachu commented Jul 11, 2011

We'd more prefer them to be squashed into single commit. The commit (patch) should coming with test case, documentation, implementation, and guide update (If applicable.) Would you mind squashed them together again.

Thanks a lot. :)

Grouped select helper
FormBuilder#select now works with a nested data structure.

AndrewRadev commented Jul 11, 2011

Okay then, I squashed them into a single commit and rebased against master again. Thanks for merging :).

josevalim added a commit that referenced this pull request Jul 11, 2011

Merge pull request #1722 from AndrewRadev/grouped-select
Make "select" helper handle nested collections

@josevalim josevalim merged commit 1939fa3 into rails:master Jul 11, 2011

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