Rails选择标签multiple = true isn不工作

时间:2015-12-22 16:39:44

标签: ruby-on-rails

我想要一个select标签来允许多个选择。我尝试过各种多种选择,但似乎都没有。这是我现在拥有的(Ruby 2.x,Rails 4.x)

  <div class="field">
    <%= f.label :category %><br>
    <%= f.select :category, options_for_select(@categories.sort),
:include_blank => true, :multiple => true  %>
  </div>

当我转到表单时,列出了项目,但我无法使用Control或Shift键选择多个项目。

我的搜索模型是

 has_many :documents

  def search_documents

    documents = Document.all

    documents = documents.where("document_title like ?", "%#{document_title}%") if document_title.present?
    documents = documents.where("summary like ?", "%#{summary}%") if summary.present?
    documents = documents.joins(:category).where("categories.name like ?", "%#{category}%") if category.present?
    documents = documents.joins(:owner).where("owners.name like ?", "%#{owner}%") if owner.present?
    documents = documents.where("doc_file_file_name like ?", "%#{file_name}%") if file_name.present?


    return documents
  end

如果我查看结果页面的来源,则表明该多重页面无效

<div class="field">
    <label for="search_category">Category</label><br>xxx
    <select name="search[category]" id="search_category"><option value=""></option>
<option value="Apples">Apples</option>
<option value="Calendar">Calendar</option>
<option value="Cct Catalogs">Cct Catalogs</option>
<option value="Forms">Forms</option>
<option value="Sell Sheets">Sell Sheets</option></select>
  </div>

我相信我在搜索多个项目时会遇到问题但是现在,我只是想在下拉列表问题中解决多选问题。

1 个答案:

答案 0 :(得分:2)

更改

someMethod(false)  // method is invoked
someMethod(false)  // method is not invoked, result is taken from cache
someMethod(true)   // method is invoked
someMethod(false)  // method is unnecessarily invoked
someMethod(false)  // method is not invoked, result is taken from cache

<%= f.select :category, options_for_select(@categories.sort),
    :include_blank => true, :multiple => true  %>

需要在单个哈希中定义所有Rails <%= f.select :category, options_for_select(@categories.sort), {:include_blank => true}, {:multiple => true} %> ,并且需要在单独的单个哈希中定义所有options

  

Documentation
  选择(对象,方法,选项= nil,options = {},html_options = {},&amp; block)