2016-09-28 86 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]

相关问题