javascript - Rails 没有为 JavaScript 请求选择正确的布局

标签 javascript ruby-on-rails ajax mootools

我的应用程序中有两种不同的布局,一种用于 javascript (AJAX) 请求,另一种用于常规请求。

# application.html.haml for non-js requests (abbreviated)
!!!
%html
  %head
  %body
    = yield

# and application.js.coffee for js requests
App.modal """<%= yield %>""" # creates a javascript modal

任何带有 :remote => true 的链接理论上都应该使用 javascript 布局。这在某些情况下有效,但在其他情况下不起作用。

它适用于此链接:

%li= link_to "Login", new_user_session_path, remote: true

# log output:
Started GET "/users/sign_in?&authenticity_token=KwyFmzGgR7Rdx3dudJDvw8b5rngvVDrwfTpYLPIPjEI=" for 127.0.0.1 at 2012-01-27 03:29:41 -0500
Processing by Devise::SessionsController#new as JS
  Parameters: {"authenticity_token"=>"KwyFmzGgR7Rdx3dudJDvw8b5rngvVDrwfTpYLPIPjEI="}
  Rendered devise/shared/_links.erb (0.9ms)
  Rendered devise/sessions/new.html.haml within layouts/application (6.3ms)
Completed 200 OK in 167ms (Views: 165.9ms | ActiveRecord: 0.0ms)

# output in the javascript console:
XHR finished loading: "http://localhost:3000/users/sign_in?&authenticity_token=KwyFmzGgR7Rdx3dudJDvw8b5rngvVDrwfTpYLPIPjEI=".

但不适用于此:

%li= link_to "Account", edit_user_registration_path, remote: true

# log output:
Started GET "/users/edit?&authenticity_token=KwyFmzGgR7Rdx3dudJDvw8b5rngvVDrwfTpYLPIPjEI=" for 127.0.0.1 at 2012-01-27 03:31:24 -0500
Processing by Devise::RegistrationsController#edit as JS
  Parameters: {"authenticity_token"=>"KwyFmzGgR7Rdx3dudJDvw8b5rngvVDrwfTpYLPIPjEI="}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  MobilePhone Load (0.3ms)  SELECT "mobile_phones".* FROM "mobile_phones" WHERE "mobile_phones"."user_id" = 1 LIMIT 1
  Rendered devise/registrations/edit.html.haml within layouts/application (6.7ms)
Completed 200 OK in 157ms (Views: 154.4ms | ActiveRecord: 0.6ms)

# output in the javascript console:
XHR finished loading: "http://localhost:3000/users/edit?&authenticity_token=KwyFmzGgR7Rdx3dudJDvw8b5rngvVDrwfTpYLPIPjEI=".

经过一些简单的调试,我意识到第二个请求正在命中 application.html.haml (错误!),而第一个请求正在命中 application.js.coffee >(正确!)。两者均已被 ajax 成功处理。

我在这里有点困惑。我希望我犯了一个简单的错误,有人能够指出!

谢谢!

附注我正在运行 Rails 3.2.1(之前在 3.1.3 上尝试过,但有同样的问题)

编辑:不确定这是否有什么不同,但我正在使用 mootools-rails 驱动程序:https://github.com/kevinvaldek/mootools-ujs/blob/master/Source/rails.js 。接受 header 已正确设置为“text/javascript”。

最佳答案

我遇到了同样的问题,我从头开始尝试使用新的 rails 3.2.7 并使用 application.haml 而不是 application.html .erb,远程链接不起作用。我尝试了这个解决办法:

  1. application.haml转换为部分,将application.haml重命名为_application.haml
  2. application.html.erb中只有一行:

    <%= render 'application' %>
    

关于javascript - Rails 没有为 JavaScript 请求选择正确的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9030826/

相关文章:

javascript - 如何用nodejs打包二维盒子?

ruby-on-rails - Rails 的即时通讯实现?

ruby-on-rails - Rails 4 Postgresql 数组数据类型 : updating values

javascript - 带参数的 Ajax/PHP 上传(再次...)

ajax - 等待 2 个 AJAX 请求完成(d3.js)并优化临时等待?

javascript - Home Baked Google Instant(只是花哨的 AJAX)

javascript - 为什么 RegEx.test 会在后续调用中更改结果?

javascript - "Uncaught TypeError: Cannot read property ' 添加事件监听器 ' of null"

javascript - jQuery 首先切换隐藏文本

ruby-on-rails - Mongoid::错误::混合关系