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 -%>
>%= image_tag 'spinner_arrows.gif' %< Saving...
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.
Featured writing
When your brilliant idea meets organizational reality: a survival guide
Transform your brilliant tech ideas into reality by navigating organizational challenges and overcoming hidden resistance with this essential survival guide.
Server-Side Dashboard Architecture: Why Moving Data Fetching Off the Browser Changes Everything
How choosing server-side rendering solved security, CORS, and credential management problems I didn't know I had.
AI as Coach: Transforming Professional and Continuing Education
Transform professional and continuing education with AI-driven coaching, offering personalized support, accountability, and skill mastery at scale.
Books
The Work of Being (in progress)
A book on AI, judgment, and staying human at work.
The Practice of Work (in progress)
Practical essays on how work actually gets done.
Recent writing
The bully pulpit: why AI slop only matters to people who write about AI slop
This article exposes how the 'AI moral crisis' narrative is amplified by the very people who control media—and why the 90% of workers actually using AI don't share the panic.
Why your job matters more than mine: the selective morality of job loss
This article reveals the uncomfortable pattern behind which jobs get moral protection and which get called 'market forces'—and what that means for everyone outside the creative class.
AI in writing: the end of a professional monopoly
This article reframes the AI writing debate: the panic isn't about creativity—it's about a professional class losing control of the systems they've gatekept for a century.
Notes and related thinking
Google Criticized for Privacy Issues
Explore the critique of Google’s privacy practices as Ian Hickson defends the company's intentions and highlights the impact of public skepticism.
Llama 2 avoids errors by staying quiet, GPT-4 gives long, if useless, samples
Discover how Llama 2 outperforms GPT-4 in generating reliable code, revealing crucial insights on the effectiveness of large language models.
NoMethodError (undefined method `finder') with Engines and Rails 2.2
Fix the NoMethodError with ActionMailer in Rails 2.2 by applying a simple patch. Save time and troubleshoot efficiently with our guide.