How to Include Blank Field in Select_Tag

Adding blank option to rails select tag

Your parameters at options_for_select is wrong, I think that's the right way:

<%= select_tag :equipment,
options_for_select(Equipment.all.collect { |e|
["#{e.model} - #{e.serialNum}", e.id,
{ :class =>"#{e.handReceipt}" }]}),
:include_blank => true,
:id => 'equipment' %>

Read more at:

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

How do I set a blank value for an f.select form field

There are two possibilities, depending on what you're after:

include_blank

<%= f.select (:sex, %w{ Male Female }, :include_blank => true) %>

This will always include a blank option in the select, which will allow people to set the value back to the blank value if they're seeing this on an edit form.

prompt

<%= f.select (:sex, %w{ Male Female }, :prompt => "Gender...") %>

This will include the specified prompt value, so long as the field hasn't already been set. If it has (on an edit form for example), there's no need to remind the user that they need to select a value so the prompt doesn't appear

jQuery adding blank option field in the select box

First create the empty option, then add the rest with .append()

$(document).ready(function () {
$("#select1").change(function(){
$('#trblock').fadeIn();
if ($(this).data('options') == undefined) {
$(this).data('options', $('#select2 option').clone());
}
var id = $(this).val();
var options = $(this).data('options').filter('[value=' + id + ']');
$('#select2').html('<option value="">').append(options);
});
});

Include blank option on a condition in select

You can pass to include_blank option any helper methods or conditions

= form_for country do |f|
= f.select :rating,
Rating.options,
include_blank: f.object.rating.empty?

How to name the blank value in select?

It depends on how you are constructing your options for select. If you're doing it like the code below, just pass a string into the :include blank.

select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'Some text here'})

If you're setting the options with a options_for_select(), then you can do something like the following:

options_for_select([["Dollar", "$"], ["Kroner", "DKK"]])

With the value="" being the second value in the array and the name that shows up in the dropdown being first. So in your case, you could change the second answer to look like this:

options_for_select([["Some text here", ""], ["Dollar", "$"], ["Kroner", "DKK"]])

default select option as blank

Maybe this will be helpful

<select>
<option disabled selected value> -- select an option -- </option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>

Include blank for first item in select list in options_for_select tag

I think you want this format:

select("model_name", "model_id", Model.all.collect {|mt| [ mt.name, mt.id ] }, {:include_blank => 'name of your blank prompt'})

BTW: was assuming Modle was suppose to be Model. To use using collection_select:

collection_select(:model, :model_id, Model.all, :id, :name, :prompt => true)

Formtastic - customise blank option in select tag

maybe options :prompt could help you?

  form.input :organization,  
:collection => Organization.all(:order => :title),
:prompt => "some text"

have a good day!

Select tag include blank behavior in rails 3.1 rc4

Now the code for the select tag is (Rails 3.1.0.rc4)

  def select_tag(name, option_tags = nil, options = {})
html_name = (options[:multiple] == true && !name.to_s.ends_with?("[]")) ? "#{name}[]" : name

if options.delete(:include_blank)
option_tags = "<option value=\"\"></option>".html_safe + option_tags
end

if prompt = options.delete(:prompt)
option_tags = "<option value=\"\">#{prompt}</option>".html_safe + option_tags
end

content_tag :select, option_tags, { "name" => html_name, "id" => sanitize_to_id(name) }.update(options.stringify_keys)
end

which means you have to replace :include_blank with :prompt. Use include blank if you want a blank option in your select field :)



Related Topics



Leave a reply



Submit