在更改 select 框时搜索提交表单

Ransack submit form onchange of select box

使用 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

Html 选项散列应作为单独的参数传递。更多信息请点击此处:http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select

你可以试试:

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

祝你好运!