Polymathic

Digital transformation, higher education, innovation, technology, professional skills, management, and strategy


Getting a remote form to submit within a partial collection when any select changes in Ruby on Rails

This one is harder than it seems. But, I figured out a way. The trick and breakthrough came from Teflon Ted.

With a regular form, you could do this in your select statement:

:onChange=>"this.form.submit();"

This won’t work with a remote form, because the submission is not handled with the submit method but rather within the JavaScript callback in onsubmit. So, with a remote form, you have to change it to this:

:onChange=>"this.form.onsubmit();"

So, here is my code.

<%- remote_form_for
  :user,
  user,
  :url=>{:action=>'update_remote', :id=>user.id},
  :html=>{:id=>'form_'+user.id.to_s},
  :loading=>"Element.show('spinner_"+user.id.to_s+"'); Form.disable('form_"+user.id.to_s+"')"
  do |f|
-%>

<%= f.select
  :project_id,
  Project.find(:all).collect{|p| [p.name,p.id]},
  {:include_blank=>false, :selected=>user.project_id},
  {:onChange=>"this.form.onsubmit();", :id=>'user_project_id_'+user.id.to_s}
%>

<%- end -%>


Note that this is in a partial that gets iterated over a collection. So, I have to give everything a unique id in the HTML. Also, note that this includes the user of a spinner to show activity is taking place. I also like to disable the form temporarily to make sure nothing else gets selected. I don’t have to hide the spinner or reactivate the form because when the table row gets regenerated via RJS, it goes back to the default condition.


Discover more from Polymathic

Subscribe to get the latest posts sent to your email.



4 responses to “Getting a remote form to submit within a partial collection when any select changes in Ruby on Rails”

  1. Excellent, and thanks for the cred 🙂

  2. Thanks. I had been searching why this didn’t work with the onchange event.

  3. Nice call. thanks for teh code snippet, too

  4. Can I have same problem solved using PHP??

Leave a Reply

Your email address will not be published. Required fields are marked *

About Me

Visionary leader driving digital transformation across higher education and Fortune 500 companies. Pioneered AI integration at Emory University, including GenAI and AI agents, while spearheading faculty information systems and student entrepreneurship initiatives. Led crisis management during pandemic, transitioning 200+ courses online and revitalizing continuing education through AI-driven improvements. Designed, built, and launched the Emory Center for Innovation. Combines Ph.D. in Philosophy with deep tech expertise to navigate ethical implications of emerging technologies. International experience includes DAAD fellowship in Germany. Proven track record in thought leadership, workforce development, and driving profitability in diverse sectors.

Favorite sites

  • Daring Fireball

Favorite podcasts

  • Manager Tools

Newsletter

Newsletter