diff options
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_view/helpers/form_options_helper.rb | 15 | ||||
-rw-r--r-- | actionpack/test/template/form_options_helper_test.rb | 13 |
2 files changed, 28 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 67be4d81f0..5b026245da 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -53,6 +53,21 @@ module ActionView # <option value="2">Sam</option> # <option value="3">Tobias</option> # </select> + # + # Like the other form helpers, +select+ can accept an <tt>:index</tt> option to manually set the ID used in the resulting output. Unlike other helpers, +select+ expects this + # option to be in the +html_options+ parameter. + # + # Example: + # + # select("album[]", "genre", %w[rap rock country], {}, { :index => nil }) + # + # becomes: + # + # <select name="album[][genre]" id="album__genre"> + # <option value="rap">rap</option> + # <option value="rock">rock</option> + # <option value="country">country</option> + # </select> module FormOptionsHelper include ERB::Util diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 8ee601f478..f3ecc18233 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -31,6 +31,7 @@ class FormOptionsHelperTest < Test::Unit::TestCase Continent = Struct.new('Continent', :continent_name, :countries) Country = Struct.new('Country', :country_id, :country_name) Firm = Struct.new('Firm', :time_zone) + Album = Struct.new('Album', :id, :title, :genre) end def test_collection_options @@ -305,6 +306,18 @@ class FormOptionsHelperTest < Test::Unit::TestCase select("post", "category", %w( abe <mus> hest ), :selected => 'abe') ) end + + def test_select_with_index_option + @album = Album.new + @album.id = 1 + + expected = "<select id=\"album__genre\" name=\"album[][genre]\"><option value=\"rap\">rap</option>\n<option value=\"rock\">rock</option>\n<option value=\"country\">country</option></select>" + + assert_dom_equal( + expected, + select("album[]", "genre", %w[rap rock country], {}, { :index => nil }) + ) + end def test_select_with_selected_nil @post = Post.new |