0

Is there a way to replace a text_field_tag with a select in a rails3 search function?

My current search form:

<%= form_tag orders_path, :method => 'get', :id => "orders_search" do %>
<p><%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %></p>

I wanted to replace with a dropdown to limit / filter my customers results. I've tried this:

    <%= select :search, params[:search], ([["Pending"], ["Open"], ["Closed"]]) %>       

However this gives me a 500 error and an output in the log:

TypeError (expected Array (got String) for param `search'):

I've also tried:

    <%= select :search, ([["Pending"], ["Open"], ["Closed]]) %>     

Which leads to an invalid number of arguments.

What's the best way to do this?

Jenny Blunt
  • 1,576
  • 1
  • 18
  • 41

1 Answers1

6

Check Select helper methods in Ruby on Rails out.

You can probably do what you need with:

<%= select_tag "search", options_for_select([ "Pending", "Open", "Closed" ], params[:search]) %>  

-- EDIT --

In order to submit form on select add following in your application.js (provided you use jQuery):

$(document).ready(function() {

  $("select#search").change(function(){
    $(this).closest("form").submit();
  });

});

There are already few stack overflow answers in regards to this.

Community
  • 1
  • 1
Krule
  • 6,468
  • 3
  • 34
  • 56