为什么我的远程 AJAX form_for 提交为 HTML 而不是 JS?
我已经将我的网站从 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="✓" />
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为,你应该在控制器中给出格式:
I think, you should give the format in controller:
这发生在我身上,我发现如果我专门包含
在 application.html.erb 中,那么它会破坏我的远程表单。我建议你尝试一次删除一个 js 脚本,直到它再次工作。您还需要避免:
如果您已经安装了 jQuery。
This happened to me, and I found that if I specifically include
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:
if you've installed jQuery.