ardochhigh ardochhigh - 2 months ago 21
HTML Question

Ransack submit form onchange of select box

Using Ransack, I have a simple dropdown for filtering a list. It is working OK, but I need to press the submit button.

I'd like the action of changing the dropdown to submit the search_form_for

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

The onchange doesn't work

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


The onchange doesn't appear in the rendered form

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


It looks like search_form_for just passes the options into form_for


Html options hash should be passed as a separate parameter. More information about it here:

You can try:

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

Best of luck!