Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 3174

Show
Ignore:
Timestamp:
11/23/05 21:59:20 (3 years ago)
Author:
bitsweat
Message:

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. Closes #2991.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r3163 r3174  
    11*SVN* 
     2 
     3* 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>] 
    24 
    35* Initialize @optional in routing code to avoid warnings about uninitialized access to an instance variable. [Nicholas Seckar] 
  • trunk/actionpack/lib/action_view/helpers/form_options_helper.rb

    r3156 r3174  
    4848      # could become: 
    4949      # 
    50       #   <select name="post[person_id"> 
     50      #   <select name="post[person_id]"> 
    5151      #     <option></option> 
    5252      #     <option value="1" selected="selected">David</option> 
     
    6060      # This allows the user to submit a form page more than once with the expected results of creating multiple records. 
    6161      # In addition, this allows a single partial to be used to generate form inputs for both edit and create forms. 
     62      # 
     63      # By default, post.person_id is the selected option.  Specify :selected => value to use a different selection 
     64      # or :selected => nil to leave all options unselected. 
    6265      def select(object, method, choices, options = {}, html_options = {}) 
    6366        InstanceTag.new(object, method, self, nil, options.delete(:object)).to_select_tag(choices, options, html_options) 
     
    297300        html_options = html_options.stringify_keys 
    298301        add_default_name_and_id(html_options) 
    299         content_tag("select", add_options(options_for_select(choices, value), options, value), html_options) 
     302        selected_value = options.has_key?(:selected) ? options[:selected] : value 
     303        content_tag("select", add_options(options_for_select(choices, selected_value), options, value), html_options) 
    300304      end 
    301305 
  • trunk/actionpack/test/template/form_options_helper_test.rb

    r3003 r3174  
    284284    ) 
    285285  end 
     286   
     287  def test_select_with_selected_value 
     288    @post = Post.new 
     289    @post.category = "<mus>" 
     290    assert_dom_equal( 
     291      "<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>", 
     292      select("post", "category", %w( abe <mus> hest ), :selected => 'abe') 
     293    ) 
     294  end 
     295 
     296  def test_select_with_selected_nil 
     297    @post = Post.new 
     298    @post.category = "<mus>" 
     299    assert_dom_equal( 
     300      "<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>", 
     301      select("post", "category", %w( abe <mus> hest ), :selected => nil) 
     302    ) 
     303  end 
    286304 
    287305  def test_collection_select