Archive for March 10th, 2008

:include_blank => options

Mar 10, 2008 in Ruby, Web Development

So one minor thing I found annoying while working on Heelshousing was that, options_for_select, and options_from_collection_for_select didn’t accept an option or argument for :include_blank. So I created a little file called “include_blank_options.rb” that I threw in my “config/initializers” directory. Here’s the code:

module ActionView::Helpers::FormOptionsHelper
         
   def options_for_select_with_include_blank(container, selected = nil, include_blank = false)
      options = options_for_select_without_include_blank(container, selected)
      if include_blank
         options = "<option value=\"\">#{include_blank if include_blank.kind_of?(String)}</option>\n" + options
      end
      options
   end
   alias_method_chain :options_for_select, :include_blank
   
   def options_from_collection_for_select_with_include_blank(collection, value_method, text_method, selected = nil, include_blank = false)
      options = options_from_collection_for_select_without_include_blank(collection, value_method, text_method, selected)
      if include_blank
         options = "<option value=\"\">#{include_blank if include_blank.kind_of?(String)}</option>\n" + options
      end
      options
   end
   alias_method_chain :options_from_collection_for_select, :include_blank
   
end

Tags: , , ,