开发者

Easy way to turn a Rails form into an AJAX form?

开发者 https://www.devze.com 2023-01-19 13:43 出处:网络
I have the following form in my Rails application: <% form_tag password_resets_path, :id => \'recoverPasswordForm\' do %>

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 => 'recoverPas开发者_Go百科swordForm', :ajax => true do %>

I'm using Rails 2.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号