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

Ticket #2991: select_not_selected_option.diff

File select_not_selected_option.diff, 3.2 kB (added by jonathan@bluewire.net.nz, 3 years ago)

Add :not_selected option to select() form helper (and test)

  • actionpack/test/template/form_options_helper_test.rb

    old new  
    283283      select("post", "category", %w( abe <mus> hest), :prompt => true, :include_blank => true) 
    284284    ) 
    285285  end 
     286   
     287  def test_select_with_not_selected 
     288    @post = Post.new 
     289    @post.category = "<mus>" 
     290    assert_dom_equal( 
     291      "<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>", 
     292      select("post", "category", %w( abe <mus> hest ), :not_selected => true) 
     293    ) 
     294  end 
    286295 
    287296  def test_collection_select 
    288297    @posts = [ 
  • actionpack/lib/action_view/helpers/form_options_helper.rb

    old new  
    4747      # 
    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> 
    5353      #     <option value="2">Sam</option> 
     
    5959      # to the database. Instead, a second model object is created when the create request is received. 
    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      # The option :not_selected => true can be used if the current model value should not be selected by default. 
    6264      def select(object, method, choices, options = {}, html_options = {}) 
    6365        InstanceTag.new(object, method, self, nil, options.delete(:object)).to_select_tag(choices, options, html_options) 
    6466      end 
     
    296298      def to_select_tag(choices, options, html_options) 
    297299        html_options = html_options.stringify_keys 
    298300        add_default_name_and_id(html_options) 
    299         content_tag("select", add_options(options_for_select(choices, value), options, value), html_options) 
     301        content_tag("select", add_options(options_for_select(choices, options[:not_selected] ? nil : value), options, value), html_options) 
    300302      end 
    301303 
    302304      def to_collection_select_tag(collection, value_method, text_method, options, html_options) 
  • actionpack/CHANGELOG

    old new  
    11*SVN* 
    22 
     3* Add :not_selected option to select() to not select current model value. [Jonathan Viney <jonathan@bluewire.net.nz>] 
     4 
    35* Initialize @optional in routing code to avoid warnings about uninitialized access to an instance variable. [Nicholas Seckar] 
    46 
    57* Make ActionController's render honor the :locals option when rendering a :file. #1665. [Emanuel Borsboom, Marcel Molina Jr.]