How to automatically send a Rails formhelper when onchange happens in the select drop-down list (populated from the database)

I have a form in my view with two drop-down menus populated from db tables where I would like to automatically submit the form when the onchange event occurs in any drop-down list:

<%= form_for @products, url: products_path, method: :get do |f| %> <!-- POPULATE DROPDOWNS FROM DB DATA --> <%= select('post', 'state', @suppliers.collect {|s| [s.supp_state, s.id]}) %> <%= select('post', 'category_id', @categories.collect {|c| [c.cat_name, c.id]}) %> <div class='actions inline'><%= f.submit 'GO!' %></div> <% end %> 

I tried the options for the following, but I'm brand new in Ruby / Rails / JS .. so far no luck:

 <%= select('post', 'state', @suppliers.collect {|s| [s.supp_state, s.id]}, :onchange => "this.form.submit()") %> <%= select('post', 'state', @suppliers.collect {|s| [s.supp_state, s.id]}, :input_html => {:onchange => "this.form.submit()") %> 

Any advice is appreciated!

+4
source share
1 answer

See documentation: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select

The arguments are as follows:

 select(object, method, choices, options = {}, html_options = {}) 

:onchange belongs to the html parameters, so you need to fix your selection box:

 select('post', 'state', @suppliers.collect {|s| [s.supp_state, s.id]}, :onchange => "this.form.submit()") 
+11
source

Source: https://habr.com/ru/post/1491507/


All Articles