Executing action without reloading

Hello, I need to execute an action when the user click a button but I
don't want the page to be reloaded. Is there any way to call a
controller action without reloading?



Rails 3: link_to ...., :remote => true

Rails 2.*: link_to_remote ....

Garrett Lancaster


Hello Garrett,

Ok, the link is actually a form submit button, how do I use this
remote options on a form submit?

Same syntax in the form.

form_for @whatever, :remote => true


Ok, let's see. My form declaration is:
<% form_for :support, @support, :url => { :controller =>
"concepts", :action => "sendContactMail", :id => 0, :type_id =>
@contactType.id, :property_id => @property.id, :remote => true } do |
fContact> %>
   <%= fContact.submit "Enviar mensaje", :onclick => "return
validateInterestedForm();" %>
<% end %>

Setting the remote as shown above doesn't seems to work, what would be
the right form?
Another question, in the action that will be executed (senContactMail)
must I set something special (I guess I have to remove the redirect_to
but is there anything else I have to do?).

Thanks for your help, I never used AJAX before and am actually quite
interested in learning how to use it.

The remote is part of the form not the url. Put it outside the url section.

<% form_for :support, @support, :url => { :controller =>
"concepts", :action => "sendContactMail", :id => 0, :type_id =>
@contactType.id, :property_id => @property.id }, :remote => true do |
fContact> %>

<% form_for :support, @support, :url => { :controller =>
"concepts", :action => "sendContactMail", :id => 0, :type_id =>
@contactType.id, :property_id => @property.id }, :html => { :remote =>
true } do |fContact| %>

Without success.

Get it out of the hash, it is a stand alone value.

<% form_for :support, @support, :url => { :controller =>
“concepts”, :action => “sendContactMail”, :id => 0, :type_id =>

@contactType.id, :property_id => @property.id }, :remote =>
true do |fContact| %>

That was actually my first try and it didn't result...I changed the
form tag for "form_remote_tag" and got the folloowing form tag

<form action="/concepts/sendContactMail/0?
property_id=18&amp;type_id=3" method="post" onsubmit="new
property_id=18&amp;type_id=3', {asynchronous:true, evalScripts:true,
parameters:Form.serialize(this)}); return false;">

But the page is still reloaded.

Let's clarify, this may help.
My site present diferente kind of properties. In the "properties/show"
page I have a jquery modal the user can use to fill a few informations
and send a mail. The mail is sent using the "sendContactMail" action
of the "concepts" controller.
So the idea is using an AJAX form so the "concepts/sendContactMail"
action is executed and send the mail with the user information and the
"properties/show" page is not reloaded.

Well you could always just do:

button_to_function “submit”, remote_function(:url => { } )

Ok, the "form_remote_for" helper works great, my mistake, didn't have
the prototype.js file imported...

Thanks a lot for the help!