How to auto-submit Rails formhelper, when onchange occurs on select dropdown (populated from DB)

僤鯓⒐⒋嵵緔 提交于 2019-12-01 04:53:59

问题


I have a form in my View with two dropdowns, both populated from db tables, where I'd like to auto-submit the form when onchange event occurs on either dropdown:

<%= 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 variants of the following but I'm quite new at Ruby/Rails/JS.. no luck so far:

<%= 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 greatly appreciated!


回答1:


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

The arguments goes like this:

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

:onchange belongs to html options thus you need to fix your select box to this:

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


来源:https://stackoverflow.com/questions/17663099/how-to-auto-submit-rails-formhelper-when-onchange-occurs-on-select-dropdown-po

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!