Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

like image 720
tiswas Avatar asked Jan 26 '11 17:01

tiswas


1 Answers

You must use the select form builder:

<%= f.select(:type, [["text1", "value1"], ["text2", "value2"], ...]) %>
like image 67
tokland Avatar answered Oct 22 '22 07:10

tokland