2016-09-28 49 views
0

我有以下形式:如何在options_from_collection_for_select中預先選擇?

<%= form_tag users_path, method: :get, id: 'uco' do %> 
    <%= select_tag "country", options_from_collection_for_select(ISO3166::Country.countries.sort_by(&:name), 'un_locode', 'name'), :include_blank => true %> 
<%= submit_tag "Search" %> 
<% end %> 

當我提交表單,我結束了:

www.example.com/users?country=US

我會然後像預先選擇的形式params[:country]

但是我不知道如何將params[:country]添加到select_tag中。我嘗試過失敗:基於關this example from the apidock.

回答

1

沒關係的

<%= select_tag "country", options_from_collection_for_select(ISO3166::Country.countries.sort_by(&:name), 'un_locode', 'name', params[:country]), :include_blank => true %> 

,我這個答案解決它:

How to make the select_tag keep value of last search?

:selected => params[:country]

相關問題