How can I change a form's onsubmit event handler using rjs?

Hi all,

I have a ajax form creating by “form_remote_for”, whose url is to
create a new model by default. The page doesn’t change after the ajax
call on submit button so the form is still there after the new model
is created. But if I submit the form for the second time, I want to
update the formerly created model using an Ajax post.

I think I could render an rjs at the end of the first submit action
and the rjs should change the onsubmit event handler of the form, say:

page[:form][:onsubmit] = article_path(@article)

but it seems that this doesn’t work. Another problem is that
“article_path(@article)” will generate something like “/article/12”
with HTTP PUT method, but I need something like “new
Ajax.Request(…)”. Any idea about these two questions? Thanks.

allen wrote:

Hi all,

I have a ajax form creating by “form_remote_for”, whose url is to
create a new model by default. The page doesn’t change after the ajax
call on submit button so the form is still there after the new model
is created. But if I submit the form for the second time, I want to
update the formerly created model using an Ajax post.

I think I could render an rjs at the end of the first submit action
and the rjs should change the onsubmit event handler of the form, say:

page[:form][:onsubmit] = article_path(@article)

but it seems that this doesn’t work. Another problem is that
“article_path(@article)” will generate something like “/article/12”
with HTTP PUT method, but I need something like “new
Ajax.Request(…)”. Any idea about these two questions? Thanks.

page[:form].onsubmit = remote_function(:url => article_path(@article),
…)

Although, you are probably better off rendering a partial that replaces
your form setup they way you want for edits, instead of creation.

page[:form].update :partial => ‘edit_form’