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

tis*_*was 5 forms select ruby-on-rails

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

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

这是有效的,但是当我执行搜索时,即使用户在搜索之前选择了"跟踪",它也会默认返回给艺术家.我怎么能纠正这个?

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

<% 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 %>
Run Code Online (Sandbox Code Playgroud)

任何有助于此工作的帮助将不胜感激!

tok*_*and 6

您必须使用选择表单构建器:

<%= f.select(:type, [["text1", "value1"], ["text2", "value2"], ...]) %>
Run Code Online (Sandbox Code Playgroud)