【问题标题】:ruby search drop down红宝石搜索下拉
【发布时间】:2010-05-11 16:15:12
【问题描述】:

我的Products 模型中有一个Type 的下拉列表。

我希望能够在Products index.html.erb 中进行搜索,以便用户从下拉列表中选择一个类型,单击搜索,然后返回所有匹配该类型的产品。

当用户在文本框中输入搜索内容时,我可以获得正常的搜索方法,但当他们只是从下拉列表中选择时,我无法使其正常工作。

谁能帮忙?

【问题讨论】:

  • 宁愿这样做而不必添加更多插件

标签: ruby-on-rails ruby search drop-down-menu


【解决方案1】:

在您的控制器中:

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 %>

注意:options_for_select 接受一个对数组作为 [label, value],因此我们使用 map 来构建它。

【讨论】:

  • 我将产品中的类型存储为字符串。在产品模型中,我使用 TYPES = ["type1", "type2"] 设置类型选项
  • 我发布的代码不起作用?它会给你一个错误吗?
  • 我的问题是我无法让它实际搜索,因为我不知道如何使用下拉菜单,这就是我在 index.html.erb 中找到的类型 "submit_search", :class=> "button" %>
  • 仍然遇到问题,为#<0x72a1f50>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-28
  • 1970-01-01
  • 2019-08-09
相关资源
最近更新 更多