【问题标题】:Why is my remote AJAX form_for submitting as HTML instead of as JS?为什么我的远程 AJAX form_for 提交为 HTML 而不是 JS?
【发布时间】:2011-06-27 20:33:40
【问题描述】:

我一直在将我的网站从 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

对我做错了什么有什么想法吗?

【问题讨论】:

  • 您想在 form_for 标记中添加一个等号。可能这对这个错误没有帮助。但您应该这样做。喜欢
  • 很好,shajin -- 谢谢!遗憾地没有解决问题,但确实使代码符合标准。

标签: html ajax ruby-on-rails-3 jquery form-submit


【解决方案1】:

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

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...

【讨论】:

    【解决方案2】:

    这件事发生在我身上,我发现如果我特别包含

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

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

    <%= javascript_include_tag :all %>
    

    如果你已经安装了 jQuery。

    【讨论】:

      猜你喜欢
      • 2014-06-03
      • 2013-08-15
      • 2013-05-16
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多