好的,我在 Rails 应用程序中使用 ajax 已经有一段时间了。不知何故,在我的第一个 Rails 3.1 应用程序中,我无法让基础工作..
# application.js
//= require jquery
//= require jquery_ujs
# the form
= form_for @signup, :url => '/signup', :remote => true do |f|
= f.text_field :email, :class => 'email', :size => 26
= f.submit 'Notify me'
# the controller
def signup # route.rb has 'match '/signup' => 'controller#signup'
respond_to do |format|
format.js
end
end
# view: signup.js.erb
alert('wtf?');
现在,据我所知,提交此表单应该会触发警报框。它没有。表单提交确实会通过控制器操作,该操作会渲染模板:
Rendered teaser_pages/signup.js.erb (0.1ms)
Completed 200 OK in 8ms (Views: 7.2ms | ActiveRecord: 0.0ms)
只有没有js被触发。
UJS 助手已加载并运行(我认为),因为如果我这样做..
link_to 'click me', '#', :confirm => 'confirmation box!'
..它将按预期触发一个确认框。
我会错过什么?任何看的方向将不胜感激。
谢谢你,欧文
更新:
因此,似乎 rails 在标题中证明了错误的内容类型,text/html
而不是text/javascript
. 经过一些测试甚至:
render :js => "alert('AHAHAHAHA');", :content_type => 'text/javascript'
仍将呈现带有内容类型的标题text/html
当我运行具有相同版本的新应用程序时,jquery-rails
它可以工作。在这个应用程序中它不..
我应该在哪里寻找?