Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Introduce :selected option to the select helper. Allows you to specif…

…y a selection other than the current value of object.method. Specify :selected => nil to leave all options unselected. Closes #2991.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3174 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit d933627044ff4aeaf81087902e0e8e3cff1dd356 1 parent 26f2fbf
@jeremy jeremy authored
View
2  actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Introduce :selected option to the select helper. Allows you to specify a selection other than the current value of object.method. Specify :selected => nil to leave all options unselected. #2991 [Jonathan Viney <jonathan@bluewire.net.nz>]
+
* Initialize @optional in routing code to avoid warnings about uninitialized access to an instance variable. [Nicholas Seckar]
* Make ActionController's render honor the :locals option when rendering a :file. #1665. [Emanuel Borsboom, Marcel Molina Jr.]
View
8 actionpack/lib/action_view/helpers/form_options_helper.rb
@@ -47,7 +47,7 @@ module FormOptionsHelper
#
# could become:
#
- # <select name="post[person_id">
+ # <select name="post[person_id]">
# <option></option>
# <option value="1" selected="selected">David</option>
# <option value="2">Sam</option>
@@ -59,6 +59,9 @@ module FormOptionsHelper
# to the database. Instead, a second model object is created when the create request is received.
# This allows the user to submit a form page more than once with the expected results of creating multiple records.
# In addition, this allows a single partial to be used to generate form inputs for both edit and create forms.
+ #
+ # By default, post.person_id is the selected option. Specify :selected => value to use a different selection
+ # or :selected => nil to leave all options unselected.
def select(object, method, choices, options = {}, html_options = {})
InstanceTag.new(object, method, self, nil, options.delete(:object)).to_select_tag(choices, options, html_options)
end
@@ -296,7 +299,8 @@ class InstanceTag #:nodoc:
def to_select_tag(choices, options, html_options)
html_options = html_options.stringify_keys
add_default_name_and_id(html_options)
- content_tag("select", add_options(options_for_select(choices, value), options, value), html_options)
+ selected_value = options.has_key?(:selected) ? options[:selected] : value
+ content_tag("select", add_options(options_for_select(choices, selected_value), options, value), html_options)
end
def to_collection_select_tag(collection, value_method, text_method, options, html_options)
View
18 actionpack/test/template/form_options_helper_test.rb
@@ -283,6 +283,24 @@ def test_select_with_prompt_and_blank
select("post", "category", %w( abe <mus> hest), :prompt => true, :include_blank => true)
)
end
+
+ def test_select_with_selected_value
+ @post = Post.new
+ @post.category = "<mus>"
+ assert_dom_equal(
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\" selected=\"selected\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
+ select("post", "category", %w( abe <mus> hest ), :selected => 'abe')
+ )
+ end
+
+ def test_select_with_selected_nil
+ @post = Post.new
+ @post.category = "<mus>"
+ assert_dom_equal(
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
+ select("post", "category", %w( abe <mus> hest ), :selected => nil)
+ )
+ end
def test_collection_select
@posts = [
Please sign in to comment.
Something went wrong with that request. Please try again.