title: Using collection_select for Searching In Active Record - Rails 3.2.9
body: <p>I have only used collection_select once to populate a country id on an associated model User.  Now I would like to do a search of User using the same collection_select statement on another view and list the Users for a selected country.  My attempts have failed.  I can look at the link after a country is selected and see that the id for the country is selected.  But when I click my submit button the collection_select statement resets the selected value to the default selected value and ignores the value selected.  For example when I select the country of France, the id is 75.  When I select France and click Search by Country the id shows up like this.  </p>
  
  <pre><code>http://localhost:3000/users_admin?utf8=✓&amp;query=&amp;user%5Bcountry_id%5D=75
  </code></pre>
  
  <p>Here is the form where I have the collection_select statement.  I copied the statement that I am successfully using when I add/update a record on the User model with the selected country_id.  What I want my logic to do is when I select a country and click Search by Country that the selected country remains selected in the drop down list and the User records with the selected country_id are displayed on the screen.  The Search by Name works as expected.</p>
  
  <pre><code>&lt;%= form_tag users_admin_path, method: 'get' do %&gt;
    &lt;p style="padding-left: 20px;"&gt;
      &lt;%= text_field_tag :query, params[:query], placeholder: "Search for first, last or full name" %&gt;&amp;nbsp;&amp;nbsp;
      &lt;span valign="center"&gt;&lt;%= submit_tag "Search by Name", class: "btn btn-medium btn-custom", :name =&gt; nil %&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
      &lt;%= collection_select(:user, :country_id, Country.order('name'), :id, :name, {:selected =&gt; 233}) %&gt;&amp;nbsp;&amp;nbsp;
      &lt;span valign="center"&gt;&lt;%= submit_tag "Search by Country", class: "btn btn-medium btn-custom", :name =&gt; nil %&gt;
    &lt;/p&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Here is the code in my controller.</p>
  
  <pre><code>def users_admin
    case
    when User.where("active_user = ?", '1').count &gt; 0 # blocked users exist
      @users = User.where("active_user = ?", '1').all
    when params[:commit]=='Search by Country'
      @users = User.where("country_id = ?", params[:country_id]).all
    else
      @users = User.text_search(params[:query])
    end 
    @microposts = Micropost.all
  end
  </code></pre>
  
  <p>I'm not sure if the issue is with how the collection_select statement is coded or another logic problem.  My first thought was that I need to somehow save the selected value from the collection_select statement then use it in my where clause.  But I do not know how to recode the statement to do that and also have the default selected value as 233 which is the United States when the screen is first displayed.  I also thought that maybe I should have two different forms instead of one.  I just do not know the direction I should go at this point.</p>
  
  <p>I have searched mainly Stack Overflow for questions regarding this issue.  The questions related to collection_select for the most part were relating to uses that are way past what I will probably ever use it for.  Again I have only used the collection_statement once:)</p>
  
  <p>Any help will be appreciated.</p>