红宝石搜索下拉菜单
我在Products
模型中有一个下拉列表Type
。红宝石搜索下拉菜单
我希望能够在Products
index.html.erb中进行搜索,以便用户从下拉列表中选择一种类型,单击搜索并返回所有匹配该类型的产品。
我可以在正常的搜索方法中工作,用户在文本框中输入他们的搜索内容,但是当他们从下拉列表中选择时,我无法使其工作。
任何人都可以帮忙吗?
在你的控制器:
def index
@products = Product.all :conditons => {:type => params[:type]}
end
在视图:
<% 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:本options_for_select接受对数组作为[标号,值],因此我们使用图来建立它。
我将产品类型存储为字符串。 在产品模型中,我为 TYPES = [“type1”,“type2”] – user338454 2010-05-11 16:27:37
设置类型的选项而我发布的代码不起作用?它会给你一个错误? – 2010-05-11 16:29:42
我的问题是我不能得到它实际上搜索,因为我不知道如何与下拉,这是我在index.html.erb find type “submit_search”,:class =>“button”%> – user338454 2010-05-11 16:35:47
宁愿这样做,而无需添加更多插件 – user338454 2010-05-11 16:16:20