将 Rails 表单转换为 AJAX 表单的简单方法?
我的 Rails 应用程序中有以下表单:
<% form_tag password_resets_path, :id => 'recoverPasswordForm' do %>
<label for="passwordRecoveryEmailAddress">Email Address:</label>
<%= text_field_tag "passwordRecoveryEmailAddress" %>
<%= submit_tag 'Recover' %>
<br />
<div id="forgotPasswordLoginLinkContainer">
<a id="forgotPasswordLoginLink" href="/login">Login Instead</a>
</div>
<% end %>
提交此表单时,必须重新加载页面。我想轻松地将此表单转换为 AJAX 表单,以便该表单通过 AJAX 提交,并且不会发生页面重新加载。
我可以使用 jQuery 轻松完成此操作,连接到 .submit() 函数。但是,我很好奇:Rails 是否提供了一些简单的方法将任何给定的表单转换为 AJAX 表单?或者,最简单(但优雅)的方法是什么?也许就像
<% form_tag password_resets_path, :id => 'recoverPasswordForm', :ajax => true do %>
我正在使用 Rails 2 一样。
I have the following form in my Rails application:
<% form_tag password_resets_path, :id => 'recoverPasswordForm' do %>
<label for="passwordRecoveryEmailAddress">Email Address:</label>
<%= 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,你的猜测很接近。 Rails 3 允许您执行
form_tag ..., :remote =>; true
让表单使用 AJAX(如果 Javascript 可用)。请参阅 http://railsapi.com/doc/ Rails-v3.0.0/classes/ActionView/Helpers/FormTagHelper.html#M002483
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