Ruby on rails does not select a default value

I am currently using select as follows (inside 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 returns to the artist by default, even if the user selects a track before searching. How can i fix this?

Automatic selection of the current value works for the switches:

<% 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 letting this work would be greatly appreciated!

+5
source share
1 answer

You should use the select form constructor :

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

All Articles