The method options_from_collection_for_select
accepts an optional selected
argument, which marks that <option>
as selected. Update your code as follows:
<%= form_tag "/airports/#{@airport.id}/tablo_out/", :method => 'get', :id => "timetableaps_sear$
<%= @selected = params[:search]%>
<%= select_tag "search_al", options_from_collection_for_select(@t.map(&:aircompany).uniq.sort_by(&:airline_name), :id, :airline_name, params[:search_al]), prompt: "All Airlines", onchange: "this.form.submit();"%>
<%= select_tag "search", options_from_collection_for_select(@t.group("apname").order("cityname"),"way_end", "apname", @selected), prompt: "All Airports", onchange: "this.form.submit();" %>
<%= submit_tag "Search", name: nil %>
<% end %>
See the API for additional information: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_from_collection_for_select
A few other nitpicks and advice:
Choose a syntax, and use it every time, at least in the same file/method/block/etc. Why? Looks cleaner... Example:
:this => "that" vs this: "that"
Use virtual attributes as much as possible from the controller.Why? Looks cleaner AND if you decide to change how you retrieve @airlines, it's easier to do it in the controllers then tracking it down in the views. Example:
@t.map(&:aircompany).uniq.sort_by(&:airline_name) vs @airlines
Personal preference, I try to never use onchange
, instead I separate view and js using event-listeners. Why? Looks cleaner and I'm able to re-use the same js across the app WHILE only having to change it in one place.