无法让 UJS 在 Rails 3.1 中工作

发布于 2024-12-05 12:34:19 字数 1303 浏览 1 评论 0原文

好的,我在 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!'

..它将按预期触发确认框。

我可能会错过什么?任何方向看将不胜感激。

谢谢你, Erwin

更新:

所以看来 Rails 证明了标头中的内容类型错误,提供了 text/html 而不是 text/javascript。经过一些测试后,甚至:

render :js => "alert('AHAHAHAHA');", :content_type => 'text/javascript'

仍会呈现内容类型为 text/html 的标头

当我使用相同版本的 jquery-rails 运行新应用程序时,它可以工作。在此应用程序中,它不..

我应该在哪里寻找?

Ok, i've been using ajax in my rails apps for quite some time. Somehow, in my first Rails 3.1 app I can't get the basics to work..

# 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?');

Now, from what I can tell, submitting this form should trigger the alert box. It doesn't. The form submission does go through to the controller action, which does render the template:

Rendered teaser_pages/signup.js.erb (0.1ms)
Completed 200 OK in 8ms (Views: 7.2ms | ActiveRecord: 0.0ms)

Only no js is triggered.

The UJS helpers are loaded and functioning though (I think) because if i do..

link_to 'click me', '#', :confirm => 'confirmation box!'

..it will trigger a confirmation box as expected.

What could I be missing? Any direction to look would be appreciated.

Thank you,
Erwin

UPDATE:

So it seems rails is proving the wrong content-type in the header, providing text/html instead of text/javascript. After some testing even:

render :js => "alert('AHAHAHAHA');", :content_type => 'text/javascript'

will still render a header with content-type text/html

When I run a fresh application with the same version of jquery-rails it works. In this application it doesn't..

Where should I be looking?

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

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

发布评论

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

评论(1

私野 2024-12-12 12:34:19

我遇到了同样的问题。我也使用相同的 gemfile 设置了一个测试应用程序,并得到了与您描述的相同的结果:它在新创建的应用程序中工作,但在旧应用程序中不起作用。目前,我在实际渲染调用之前手动设置内容类型标头。

respond_to do |format|
  headers["Content-type"] = 'text/javascript'
  format.js { render "the_template" }
end

另一方面,这并没有改变正确传递的 content_type:

respond_to do |format|
  format.js { render "the_template", :content_type => 'text/javascript' }
end

希望这暂时有帮助。

您在此期间得到真正的解决方案了吗?

更新:

至少我发现了我真正的问题。我有一个 before_filter 强制 content_type 为 text/html ,这是我前一段时间为测试而添加的,但忘记停用:

def set_header
response.headers["Content-Type"] = "text/html; charset=utf-8"
end

I got the same problem. I set up a test app with same gemfile as well and got the same result as you described: it works in the newly created app but not in the old one. For the time being I set the content type header by hand before the actual render call.

respond_to do |format|
  headers["Content-type"] = 'text/javascript'
  format.js { render "the_template" }
end

This on the other hand did not change the content_type delivered correctly:

respond_to do |format|
  format.js { render "the_template", :content_type => 'text/javascript' }
end

Hope this helps for the moment.

Did you get the real solution in the meantime?

UPDATE:

At least I found my real problem. I have a before_filter forcing the content_type to text/html which I added for testing some time ago and forgot to deactivate:

def set_header
response.headers["Content-Type"] = "text/html; charset=utf-8"
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文