Jeff
2007-Sep-13 20:00 UTC
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? Thanks, Jeff --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeff
2007-Sep-13 22:08 UTC
Re: Why does an RJS call within a form cause submit the form''s submit action to be executed?
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> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ivor Paul
2007-Sep-14 06:02 UTC
Re: Why does an RJS call within a form cause submit the form''s submit action to be executed?
thanks for the post - i was wondering about this one. On 9/14/07, Jeff <addstudios-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > 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> > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
William Pratt
2007-Sep-14 14:28 UTC
Re: Why does an RJS call within a form cause submit the form''s submit action to be executed?
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 Ivor Paul wrote:> thanks for the post - i was wondering about this one. > > On 9/14/07, *Jeff* <addstudios-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > <mailto:addstudios-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> wrote: > > > 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 <http://account.id>}, > :method=>''post'', > :submit=>''my_inputs'') > %>">Add to List</button> > > > > > > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
alexey.Creopolis
2007-Sep-16 08:05 UTC
Re: Why does an RJS call within a form cause submit the form''s submit action to be executed?
*Jeff* How do u render "submit" action in controller ? respond_to ? On Sep 13, 11:00 pm, Jeff <addstud...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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? > > Thanks, > Jeff--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeff
2007-Sep-16 23:24 UTC
Re: Why does an RJS call within a form cause submit the form''s submit action to be executed?
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" <alex.creopo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> *Jeff* > How do u render "submit" action in controller ? > > respond_to ? > > On Sep 13, 11:00 pm, Jeff <addstud...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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? > > > Thanks, > > Jeff--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---