开发者

Why is my remote AJAX form_for submitting as HTML instead of as JS?

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="&#x2713;" />
    <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 w开发者_Python百科rong?


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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜