如何在options_from_collection_for_select中预先选择?

时间:2016-09-28 16:56:50

标签: ruby-on-rails

我有以下表格:

<%= 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中。我没试成功:

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

基于this example from the apidock.

1 个答案:

答案 0 :(得分:1)

没关系,我用这个答案解决了这个问题:

How to make the select_tag keep value of last search?

:selected => params[:country]