Forum: Ruby on Rails remote_form_for behavior on javascript submit();

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
(Guest)
on 2007-07-12 21:20
(Received via mailing list)
Hey all,

Tried this on the IRC channel but after an hour i figured nobody that
was on knew, so i'll try it here.  I have a rhtml page with:

<% remote_form_for(:performance_goal, :url =>
user_performance_goal_path(@user.account, @user,
@performance_goal), :html => {:id => 'sidebar_form', :method => :put})
do |f| %>

<%= f.text_area :impact, :size => '25x3', :onchange =>
"this.form.submit();" %>

<% end %>

please know that the :method => :put is in there to send the request
to my RESTful update method.  When i change the text area it does in
fact submit the form, but not through AJAX.  Instead it sends it as a
normal html form and ignores the onsubmit attribute which reads:

onsubmit="new Ajax.Request('/accounts/1/users/1/gss/performance_goals/
1', {asynchronous:true, evalScripts:true,
parameters:Form.serialize(this)}); return false;"

Ive tried using the observe_form helper and it behaves the same way.
The only way i can have it submit through AJAX is by using a submit
button.  How can i submit the remote_form_for form via javascript and
have it still perform the AJAX request?

Thanks!
peetldee (Guest)
on 2007-07-19 03:14
(Received via mailing list)
Hello Mpeychich,

This is not a rails problem nor a problem in your code.  There is
browser non-compliance to the submit() action which Steve Nyholm has
documented here,
http://blogs.vertigosoftware.com/snyholm/archive/2...,
so what occurs is submit() does not call the onsubmit event handler
that the ajax request is bound to.

I can see two potential solutions to this
(1) do not call form.submit but instead generate the ajax on your own
with something such as,
"#{remote_function(:url =>user_performance_goal_path(@user.account,
@user, @performance_goal))}; return false;"

(2) explicitly call the forms onsubmit event handler with some
javascript

The problem with solution (1) is that this WILL NOT serialize your
form elements, which is general something you want and often something
you need.  To serialize your form elements use the following,
"#{remote_function(:url =>user_performance_goal_path(@user.account,
@user, @performance_goal), :with => "Form.serialize(form)" )}; return
false;"

where "form" in the serialization is your form.

Peter

On Jul 12, 10:20 am, "removed_email_address@domain.invalid" 
<removed_email_address@domain.invalid>
This topic is locked and can not be replied to.