I found the problem! Thanks to the hints in the answers of both @Jake Smith and @Parandroid. Here's what I found in two steps.
Finding 1
While getting the ajax:success
to be fired did not seem to be the problem, it did look like the form handling did not work 100% correctly just on the $("#editor")
selector. At the very least that needed to be $("#editor form")
, but that might not work if we start from the index page, which doesn't contain the form yet. So the approach suggested by @Jake Smith seemed to be the most robust way to go after all. This resulted in:
edit.html.haml
#editor
= render :partial => "edit"
edit.js.erb
$('#editor').html('<%= escape_javascript render("edit") %>');
_edit.html.haml (still not working)
%table
- @items.each do |item|
%tr
- if item == @item
= form_for @item, :url => item_path(@item), :remote => true, do |f|
= render :partial => "row_form", :locals => { :f => f }
- else
= render :partial => 'row', :locals => { :item => item }
But still this did not result in a working submit button. Until I discovered what went wrong...
Finding 2
The solution above did give the submit button plain old POST
behavior, which the server did not like, since it expected a PUT
to reach the update
action. Rails does this by generating a hidden _method
field with the value PUT
. I found out that rails generates this field (and a couple of other crucial hidden fields) on the very top of the _edit.html.haml
partial and not inside the form
tag! So I moved the form to the top of the partial and it worked!
_edit.html.haml (WORKING!)
= form_for @item, :url => item_path(@item), :remote => true, do |f|
%table
- @items.each do |item|
%tr
- if item == @item
= render :partial => "row_form", :locals => { :f => f }
- else
= render :partial => 'row', :locals => { :item => item }
Who would have guessed...