为什么我的远程 AJAX form_for 提交为 HTML 而不是 JS?

发布于 2024-11-17 16:10:27 字数 1273 浏览 0 评论 0原文

我已经将我的网站从 Rails 2.x 转换为 Rails 3.0,到目前为止一切顺利,除了 form_for 遇到问题。我知道从 remote_form_for 切换到 form_for, :remote => true,并且让新型 AJAX 对于普通超链接运行得非常好。

不过,对于表单,我遇到了一个奇怪的问题,即表单是以 HTML 而不是 JS 的形式提交的,因此控制器会错误地处理表单。这就是我所得到的。

<% form_for (AuthorSubscription.new), :remote => true, :id => "subscribe_form" do |f| %>
    <%= f.submit "Subscribe" %>
<% end %>

其结果是 HTML

<form accept-charset="UTF-8" action="/author_subscriptions" class="new_author_subscription" data-remote="true" id="new_author_subscription" method="post">
  <div style="margin:0;padding:0;display:inline">
    <input name="utf8" type="hidden" value="&#x2713;" />
    <input name="authenticity_token" type="hidden" value="NxCF177nMDfL6QsYjesBUOUUJ9QdzKIdZYQjGAaGYmA=" />
  </div>        

  <input id="author_subscription_submit" name="commit" type="submit" value="Subscribe" /> 
</form>

您可以看到 data-remote 属性就在那里,一切看起来都很好。但记录器向我展示了

Started POST "/author_subscriptions" for 127.0.0.1 at 2011-06-27 16:21:29 -0400
  Processing by AuthorSubscriptionsController#create as HTML

我做错了什么的任何想法?

I've been converting my site from Rails 2.x to Rails 3.0, and so far so good, except for a problem I'm having with a form_for. I know to switch from remote_form_for to form_for, :remote => true, and have the new-style AJAX working fantastically well for normal hyperlinks.

With forms, though, I'm hitting a strange problem, which is that the forms are being submitted as HTML rather than JS, and are getting handled incorrectly by the controller as a result. Here's what I've got.

<% form_for (AuthorSubscription.new), :remote => true, :id => "subscribe_form" do |f| %>
    <%= f.submit "Subscribe" %>
<% end %>

which results in the HTML of

<form accept-charset="UTF-8" action="/author_subscriptions" class="new_author_subscription" data-remote="true" id="new_author_subscription" method="post">
  <div style="margin:0;padding:0;display:inline">
    <input name="utf8" type="hidden" value="✓" />
    <input name="authenticity_token" type="hidden" value="NxCF177nMDfL6QsYjesBUOUUJ9QdzKIdZYQjGAaGYmA=" />
  </div>        

  <input id="author_subscription_submit" name="commit" type="submit" value="Subscribe" /> 
</form>

You can see that the data-remote attribute is there, and all seems good. But the logger is showing me

Started POST "/author_subscriptions" for 127.0.0.1 at 2011-06-27 16:21:29 -0400
  Processing by AuthorSubscriptionsController#create as HTML

Any ideas for what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明月夜 2024-11-24 16:10:27

我认为,你应该在控制器中给出格式:

class AuthorSubscriptionsController < ApplicationController
    @@ajax_calls = [:your_ajax_method]
    respond_to :html, :except => @@ajax_calls
    respond_to :js, :only => @@ajax_calls, :layout => false

    def your_ajax_method
      etc...

I think, you should give the format in controller:

class AuthorSubscriptionsController < ApplicationController
    @@ajax_calls = [:your_ajax_method]
    respond_to :html, :except => @@ajax_calls
    respond_to :js, :only => @@ajax_calls, :layout => false

    def your_ajax_method
      etc...
蓝礼 2024-11-24 16:10:27

这发生在我身上,我发现如果我专门包含

<%= javascript_include_tag "jquery-1.6.2.min.js" %>

在 application.html.erb 中,那么它会破坏我的远程表单。我建议你尝试一次删除一个 js 脚本,直到它再次工作。您还需要避免:

<%= javascript_include_tag :all %>

如果您已经安装了 jQuery。

This happened to me, and I found that if I specifically include

<%= javascript_include_tag "jquery-1.6.2.min.js" %>

in application.html.erb, then it breaks my remote forms. I'd suggest you try removing one js script at a time until it works again. You will also want to avoid:

<%= javascript_include_tag :all %>

if you've installed jQuery.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文