user338454
user338454

Reputation: 1

ruby search drop down

I have a drop down list of Type in my Products model.

I want to be able to search in the Products index.html.erb so a user selects a type from a drop down list, clicks search and all the products matching that type are returned.

I can get normal search methods working where the user enters in their search in a text box but I cannot get it working when they just select from a dropdown.

Can anyone help?

Upvotes: 0

Views: 1558

Answers (1)

Jakub Hampl
Jakub Hampl

Reputation: 40533

In your controller :

def index
  @products = Product.all :conditons => {:type => params[:type]}
end

In your view:

<% form_tag products_path, :method => :get do %>
  <%=select_tag :type, options_for_select(Product::TYPES.map{ |type| [type, type]}), :onchange => "this.form.submit();" %>
  <%=submit_tag "Search" %>
<% end %>

NB: The options_for_select accepts an array of pairs as [label, value], so we use map to build it.

Upvotes: 1

Related Questions