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

**URL:** <https://www.ruby-forum.com/t/how-can-i-change-a-forms-onsubmit-event-handler-using-rjs/103680>\
**Category:** Rails\
**Created:** [May 9, 2007, 6:32am UTC](https://www.ruby-forum.com/t/how-can-i-change-a-forms-onsubmit-event-handler-using-rjs/103680 "2007-05-09T06:32:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![allen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://www.ruby-forum.com/u/allen)\
**Post date:** [May 9, 2007, 6:32am UTC](https://www.ruby-forum.com/t/how-can-i-change-a-forms-onsubmit-event-handler-using-rjs/103680/1 "2007-05-09T06:32:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![allen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://www.ruby-forum.com/u/allen)\
**Post date:** [May 9, 2007, 7:24pm UTC](https://www.ruby-forum.com/t/how-can-i-change-a-forms-onsubmit-event-handler-using-rjs/103680/2 "2007-05-09T19:24:40Z")

</div>

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’
