ardochhigh ardochhigh - 3 months ago 42
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| %>
<%= 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 %>


The onchange doesn't work

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


EDIT

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




EDIT 2

It looks like search_form_for just passes the options into form_for

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

Answer

Html options hash should be passed as a separate parameter. More information about it here: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select

You can try:

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

Best of luck!

Comments