# Why does an RJS call within a form cause submit the form's s

**URL:** <https://www.ruby-forum.com/t/why-does-an-rjs-call-within-a-form-cause-submit-the-forms-s/114643>\
**Category:** Rails\
**Created:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/why-does-an-rjs-call-within-a-form-cause-submit-the-forms-s/114643 "2007-09-17T13:23:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/why-does-an-rjs-call-within-a-form-cause-submit-the-forms-s/114643/1 "2007-09-17T13:23:33Z")

</div>

I have a page with a large form. The form is created using form\_for.  
Within the form, I have several button tags (not submit tags). Each  
button has a remote\_function call in the onclick event. When a button  
is clicked, the remote function is executed, but the RJS tempate is  
not invoked. Moreover, the form’s action method is invoked on the  
server after the remote function (as though the form is being  
submitted). So there are two problems in this scenario: 1) my remote  
function is executed, but the corresponding RJS template is not  
executed; 2) the action specified by the form also fires. The code  
looks something like this:

\<% form\_for(:account, :url =\> rule\_path(@account), :html =\> {:method  
=\> :put, :id =\> ‘rules\_form’}) do |f| %\>  
…  
Add to  
List  
…  
\<% end %\>

To fix this I replacing the form\_for tag with remote\_form\_for. In  
this scenario, I get better results: the RJS template is executed  
after the remote function call. But I still have one problem: the  
form’s action methods are still executed. The code looks like this:

\<% remote\_form\_for(:account, :url =\> rule\_path(@account), :html =\>  
{:method =\> :put, :id =\> ‘rules\_form’}) do |f| %\>  
…  
Add to  
List  
…  
\<% end %\>

Is this the expected behavior when a remote function is nested inside  
a form?

Thanks,  
Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/why-does-an-rjs-call-within-a-form-cause-submit-the-forms-s/114643/2 "2007-09-17T13:24:41Z")

</div>

_Jeff_  
How do u render “submit” action in controller ?

respond\_to ?

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [September 25, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/why-does-an-rjs-call-within-a-form-cause-submit-the-forms-s/114643/3 "2007-09-25T23:00:03Z")

</div>

The problem was in my button tags: the default type attribute for a  
button is “submit”, so my button tags need to look like this:

Add to List

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [September 25, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/why-does-an-rjs-call-within-a-form-cause-submit-the-forms-s/114643/4 "2007-09-25T23:03:31Z")

</div>

When the “submit” button is clicked, the URL in the action attribute  
of the form tag will be submitted to the server. So in my example,  
the update method in the rules\_controller is executed.

On Sep 16, 4:05 am, “alexey.Creopolis” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [September 25, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/why-does-an-rjs-call-within-a-form-cause-submit-the-forms-s/114643/5 "2007-09-25T23:09:44Z")

</div>

thanks for the post - i was wondering about this one.

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [September 25, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/why-does-an-rjs-call-within-a-form-cause-submit-the-forms-s/114643/6 "2007-09-25T23:10:50Z")

</div>

You should look at button\_to\_function. It will do exactly what you want  
including setting the type to button.

[http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#M000631](http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#M000631)

Ivor P. wrote:

> ```
> :url=>{:action=>'my_thing',
> :id=>@account.id <http://account.id>},
> :method=>'post',
> :submit=>'my_inputs')
> %>">Add to List</button>
> 
> >
> 
> ```

–  
Sincerely,

William P.
