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'%>
You can use the option :include_blank => true
as documented here.
I think the right way is
:prompt=>"your text"
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'%>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With