开发者

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开发者_JS百科 %>
  <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)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜