开发者

ruby on rails select not defaulting to current value

I'm currently using a select as follows (within a form):

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

This works, but when I perform a search, it defaults back to artist even if the user selected Track before searching. How can I correct this?

Automatically selecting the current value works for radio buttons开发者_如何学C:

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

Any help getting this to work will be greatly appreciated!


You must use the select form builder:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜