Ransack提交表格在选择框的更改

时间:2016-08-23 20:34:06

标签: javascript html onchange ransack

使用Ransack,我有一个简单的下拉列表来过滤列表。它工作正常,但我需要按提交按钮。

我想更改下拉列表以提交 search_form_for

的操作
<%= search_form_for @query do |f| %>
     <%= f.select :category_eq, options_for_select(Article::CATEGORIES.sort.map {|k,v| [v,k]}), include_blank: true, onchange: "this.form.submit();" %>
     <%= f.submit  %>
<% end %>

onchange 不起作用

onchange: "this.form.submit();"

修改

onchange不会出现在呈现的表单中

 <form class="article_search" id="article_search" action="/articles" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓">
        <select name="q[category_eq]" id="q_category_eq"><option value=""></option>
<option value="caffe">Caffe</option>
<option value="data_mining">Data mining</option>
<option value="deep_learning">Deep learning</option>


        <input type="submit" name="commit" value="Search">

编辑2

看起来 search_form_for 只是将选项传递给form_for

https://github.com/activerecord-hackery/ransack/blob/26cb9e4210fd1728f266ea40095ed6093ca4a994/lib/ransack/helpers/form_helper.rb#L34

1 个答案:

答案 0 :(得分:1)

Html选项哈希应作为单独的参数传递。有关它的更多信息,请访问:http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select

您可以尝试:

<%= f.select :category_eq, ..., { include_blank: true }, { onchange: 'this.form.submit();' } %>

祝你好运!