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 ma开发者_如何学编程tching 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?
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.
精彩评论