views:

58

answers:

1

I have the following form in my Rails application:

<% form_tag password_resets_path, :id => 'recoverPasswordForm' do %>
  <label for="passwordRecoveryEmailAddress">Email Address:</label>&nbsp;
  <%= text_field_tag "passwordRecoveryEmailAddress" %>
  <%= submit_tag 'Recover' %>

  <br />

  <div id="forgotPasswordLoginLinkContainer">
    <a id="forgotPasswordLoginLink" href="/login">Login Instead</a>
  </div>
<% end %>

When this form is submitted, the page must reload. I would like to easily turn this form into an AJAX form, such that the form submits via AJAX, and a page reload does not happen.

I could do this easily using jQuery, hooking into the .submit() function. But, I am curious: does Rails provide some easy way to turn any given form into an AJAX form? Or, what's the simplest (yet elegant) way possible? Maybe something like

<% form_tag password_resets_path, :id => 'recoverPasswordForm', :ajax => true do %>

I'm using Rails 2.

+1  A: 

Yes, and you were close with your guess. Rails 3 allows you to do form_tag ..., :remote => true to let the form use AJAX if Javascript is available.

See http://railsapi.com/doc/rails-v3.0.0/classes/ActionView/Helpers/FormTagHelper.html#M002483

rspeicher
Is there a way with Rails 2? I won't be using 3 with this project, unfortunately.
Chad Johnson
Use `form_remote_tag` in that case - http://railsapi.com/doc/rails-v2.3.8/classes/ActionView/Helpers/PrototypeHelper.html#M002318
rspeicher
Nice! I will try this. Thank you!
Chad Johnson