【问题标题】:Rails select tag multiple = true isn't workingRails select tag multiple = true 不起作用
【发布时间】:2015-12-22 16:39:43
【问题描述】:

我想要一个选择标签来允许多项选择。我尝试了各种多种选择,但似乎都没有奏效。这是我现在拥有的(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>

我相信我在让搜索与多个项目一起工作时会遇到问题,但现在,我只是想解决下拉问题中的多选问题。

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    改变

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

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

    所有 Rails options 都需要在单个哈希中定义,所有 html_options 需要在单独的单个哈希中定义。

    Documentation
    select(对象、方法、选择 = nil、选项 = {}、html_options = {}、&block)

    【讨论】:

    • 几乎做到了。我将其更改为 &lt;%= f.select :category, options_for_select(@categories.sort),{:include_blank =&gt; true}, {:multiple =&gt; true} %&gt; 并且它起作用了(在 include 和 multiple 周围放置单独的哈希
    • 是的,你是对的。 include_blankoptions 的一部分,而multiplehtml_options 的一部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 2013-08-31
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多