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

Ticket #5580 (reopened enhancement)

Opened 4 years ago

Last modified 2 years ago

[PATCH][TINY] Allow include_blank for select_tag

Reported by: joeat303@yahoo.com Assigned to: David
Priority: low Milestone:
Component: ActionPack Version: edge
Severity: trivial Keywords: include_blank select_tag tiny
Cc:

Description

I think one of these tags - probably select_tag - would be nicer with the :include_blank option.

Attachments

allow_include_blank_in_select_tag.diff (2.8 kB) - added by danielmorrison on 08/07/07 19:02:25.

Change History

01/12/07 07:27:11 changed by jarkko

  • status changed from new to closed.
  • resolution set to wontfix.

select_tag is supposed to be a simple wrapper creating just the select tags, totally agnostic to what's inside. So it shouldn't probably go messing with the options inside.

In that sense, the option should be placed in options_from_collection_for_select. However, it's a very specialized method which doesn't even take an options parameter. Just adding this wouldn't really merit creating the parameter, especially given the method's specialized nature.

So we're left with no place to put the option in, and I think that's fine. If you want convenience, use select. If you have some more special needs, write a wrapper of your own that does the trick.

03/30/07 17:22:20 changed by joevandyk

It seems weird that the select function accepts the :include_blank option, but the select_tag doesn't. I've ran into a few people that have been caught by that issue (including me).

Shouldn't it accept that option, for consistency's sake?

03/30/07 20:23:23 changed by jarkko

joe: select_tag is just what its name implies. It outputs a select tag, just like form_tag outputs a form tag and doesn't care what goes inside. Creating the options inside the select element is not its job at all.

select, on the other hand, is a totally different beast that automatically creates the option elements as well. So even though their names sound similar, it doesn't merit adding parameters to select_tag that don't really belong there.

08/07/07 19:01:46 changed by danielmorrison

  • keywords changed from include_blank select_tag to include_blank select_tag tiny.
  • status changed from closed to reopened.
  • version set to edge.
  • resolution deleted.
  • summary changed from include_blank for select_tag or options_from_collection_for_select to [PATCH][TINY] Allow include_blank for select_tag.

I'm reopening this ticket with a simple, tested & documented patch.

I disagree with the discussion that select_tag shouldn't touch the options. In this case, convenience trumps purity. Also, it only adds to the options, it doesn't touch existing ones.

There is currently no easy way to include a blank option. Both options_for_select and options_from_collection_for_select get clunky when you want to add a blank option. This patch allows you to continue using those helpers, your own, or a string. It doesn't break anything.

While ticket #8677 (Add :include_blank option to options_for_select()) is a nice idea, my patch works more universally and is just as simple.

08/07/07 19:02:25 changed by danielmorrison

  • attachment allow_include_blank_in_select_tag.diff added.

02/11/08 16:19:20 changed by djanowski

Patch applies cleanly and it's a very useful feature. + 1 here.

03/28/08 19:36:15 changed by miloops

It makes complete sense to me since the feature already exists for select

But i would change

if options[:include_blank] 
  option_tags = "<option value=\"\">#{options[:include_blank] if options[:include_blank].kind_of?(String)}</option>\n#{option_tags}" 
  options.delete(:include_blank) 
end 

for:

if include_blank = options.delete(:include_blank)
  option_tags = "<option value=\"\">#{include_blank if include_blank.kind_of?(String)}</option>\n#{option_tags}" 
end 

A little bit cleaner i think

05/10/08 07:13:57 changed by zardinuk

+1

07/15/08 04:57:43 changed by kt_leohart

+1 Pretty please guys. I think it makes perfect sense for select_tag to has this same option as select.