简体   繁体   中英

rails add :prompt to form_tag fields?

My question is simple. Can I add either of the blow

:prompt => "Any"
:include_blank => true

to a form in form_tag.

Here is an example. I would like to add :prompt to the select_tag :condition and select_tag :category fields and am having trouble.

 <ul id="homepage_searchbar">
<% form_tag junklists_path, :method => :get do %>
  <li>
    <%= image_tag('search_icon.png', :id => 'main_search_icon' ) %>
  </li>
  <li>
    <%= text_field_tag :search, "I'm looking for junk called...", :id => "main_field" %>
  </li>
  <li>
       <%= select_tag :condition, options_for_select(Condition.all.collect{|condition| [condition.name, condition.id]}) %>
  </li>
  <li>
       <%= select_tag :category, options_for_select(nested_set_options(Category) {|i| "#{'-' * i.level} #{i.name}"})%>
  </li>
  <li>
    <%= submit_tag "Go!", :name => 'main_submit', :id => "main_submit" %>
  </li>
<% end %>
</div>

If I can't do it the way I want, how can I add a field at the top of the select boxes that has the text "Any" but has no value when the form is submitted?

Thanks in advance!

Look at the documentation for options_for_select . You're ultimately just passing in arrays of arrays to it, so you can prepend whatever you want to appear at the top of the list.

options_for_select([["Any", "-"]] + your_method_for_generating_your_options_list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM