ruby on rails选择不默认为当前值

时间:2011-01-26 17:11:33

标签: ruby-on-rails forms select

我目前正在使用以下选项(在表单中):

<% form_for :search, :url => search_path, :html => {:method => :get} do |f| %>
        <%=  select('search', :type, options_for_select(['Artist', 'Track'])) %>
        <%= f.text_field :query %>
<% end %>

这样可行,但是当我执行搜索时,即使用户在搜索前选择了“跟踪”,它也会默认返回给艺术家。我怎么能纠正这个?

自动选择当前值适用于单选按钮:

<% form_for @search, :url => search_path, :html => {:method => :get} do |f| %>
  <p class="radio_button">
  <%= f.label :type_track, 'Search tracks' %>
  <%= f.radio_button :type, 'Track' %>
  </p>
  <p class="radio_button">
  <%= f.label :type_artist, 'Search artists' %>
  <%= f.radio_button :type, 'Artist' %>
  </p>
  <p class="text_field">
  <%= f.label :query, 'Search query' %>
  <%= f.text_field :query, :class => 'auto_focus' %>
  </p>
  <p class="submit">
  <%= submit_tag 'Go' %>
  </p>
<% end %>

非常感谢任何有助于此工作的帮助!

1 个答案:

答案 0 :(得分:6)

您必须使用select表单构建器:

<%= f.select(:type, [["text1", "value1"], ["text2", "value2"], ...]) %>