Why does an RJS call within a form cause submit the form's submit action to be executed?

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| %>
<button onclick="<%= remote_function(
          :url=>{:action=>'my_thing', :id=>@account.id},
          :method=>'post', :submit=>'my_inputs') %>">Add to List</button>
<% 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| %>
<button onclick="<%= remote_function(
          :url=>{:action=>'my_thing', :id=>@account.id},
          :method=>'post', :submit=>'my_inputs') %>">Add to List</button>
<% end %>

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


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:

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

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

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

Ivor Paul wrote:

How do u render "submit" action in controller ?

respond_to ?

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.