Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - select_tag helper - Blank option tag

I have a select_tag populated from a @users array that I'm using to perform searches. When the user first lands on the page, I'd like it to display a blank or custom tag, rather than the first item in the array?

Is this an option using the select_tag helper? To insert a blank "" option?

My helper so far:

<%= select_tag :search_user, options_from_collection_for_select(@users, "id", "name"), :class => 'submittable'%> 
like image 879
Kombo Avatar asked Mar 01 '11 00:03

Kombo


4 Answers

You can use the option :include_blank => true as documented here.

like image 138
Alberto Santini Avatar answered Sep 18 '22 13:09

Alberto Santini


I think the right way is

:prompt=>"your text"

like image 23
Rolando Avatar answered Sep 19 '22 13:09

Rolando


So, rather than using the options_from_collections_for_select method, I was able to insert an item into my array using the options_for_select. Compared to my above code, I inserted "everyone" in the below snippet.

<%= select_tag :search_user, options_for_select(@users.collect{ |user| [user.name, user.id] }.insert(0,"Everyone")), :class => 'submittable'%>
like image 20
Kombo Avatar answered Sep 17 '22 13:09

Kombo


Ruby on Rails 4.0.4 ActionView::Helpers::FormOptionsHelper

:prompt - set to true or a prompt string. When the select element

doesn't have a value yet, this prepends an option with a generic

prompt – “Please select” – or the given prompt string.

select(“post”, “person_id”, Person.all.collect {|p| [ p.name, p.id ] }, {prompt: 'Select Person'})

could become:

<select name="post[person_id]">
  <option value="">Select Person</option>
  <option value="1">David</option>
  <option value="2">Sam</option>
  <option value="3">Tobias</option>
</select>
like image 27
webDEVILopers Avatar answered Sep 17 '22 13:09

webDEVILopers