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

Rails没有为javascript请求选择正确的布局,javascript,ruby-on-rails,ajax,mootools,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 mo

我的应用程序中有两种不同的布局,一种用于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
但对这一点不起作用:

%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驱动程序:。accepts头被正确设置为“text/javascript”。

至于布局,您应该在控制器上明确指定它。也许您可以将名称application.js.coffee更改为另一个名称,并在控制器顶部添加布局“new_name”。

如果您使用的是jQuery UJS,请尝试添加

 data-type="script"
属性添加到链接标记。这将设置jQuery请求的数据类型,而jQuery请求又设置http请求的Accepts:头。Rails使用它来猜测适合您请求的响应类型


查看此博客文章了解一些背景信息:

这是我必须做的事情,以使其正常工作:(与javascript库无关)

或者,您可以将操作的模板重命名为
index.js.haml
,而不是
index.html.haml
,这将适用于ajax请求,而无需使用
respond\u to
。然而,这意味着没有启用javascript的搜索引擎和浏览器将无法访问该页面

另一种同样有效的方法是使用
respond\u with

# application_controller.rb
respond_to :html, :js

# whatever_controller.rb
def index
  respond_with # will render the appropriate layout
end

我也遇到了同样的问题,我从头开始尝试使用新的
rails 3.2.7
,由于我使用了application.haml而不是application.html.erb,远程链接无法工作。我试过这样做:

  • application.haml转换为部分,将application.haml重命名为\u application.haml
  • 在application.html.erb中,只需一行:

    <%= render 'application' %>
    
    
    

  • 很好,但是我实际上使用的是Mootools-UJS驱动程序(),谢谢,我可以试试,但是rails不应该根据请求头自动选择合适的布局吗?请求头当前设置为“text/javascript”(使用mootools-rails驱动程序),但它使用了错误的布局。此外,我在控制器顶部添加了respond_to:js,但这也没有帮助。此外,如果用户没有启用javascript,我希望application.html.haml作为备用布局,或者用于搜索引擎
    # application_controller.rb
    respond_to :html, :js
    
    # whatever_controller.rb
    def index
      respond_with # will render the appropriate layout
    end
    
    <%= render 'application' %>