开发者

Rails 3 - select_tag helper - Blank option tag

开发者 https://www.devze.com 2023-02-14 13:05 出处:网络
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 customtag, rather than the first item i

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 "" o开发者_运维问答ption?

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号