Rails没有为JavaScript请求选择正确的布局 [英] Rails not choosing the right layout for javascript requests

查看:63
本文介绍了Rails没有为JavaScript请求选择正确的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two different layouts in my app, one for javascript (AJAX) requests and one for regular requests.

# 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布局.这确实在某些情况下有效,但在其他情况下则无效.

Any link with :remote => true should in theory use the javascript layout. This DOES work on certain occasions, but does not work on others.

它适用于此链接:

%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成功处理.

After some simple debugging, I realized that the second request is hitting application.html.haml (wrong!), while the first one is hitting application.js.coffee (correct!). Both are being processed successfully by ajax.

我在这里有些困惑.我希望我犯了一个简单的错误,有人可以指出该错误!

I am a bit stumped here. I am hoping that I am making a simple mistake that somebody will be able to point out!

谢谢!

P.S.我正在运行Rails 3.2.1(以前曾在3.1.3上尝试过相同的问题)

P.S. I am running rails 3.2.1 (and previously tried on 3.1.3 with same issue)

不确定是否会有所不同,但我使用的是mootools-rails驱动程序:

Not sure if it makes a difference, but I am using the mootools-rails driver: https://github.com/kevinvaldek/mootools-ujs/blob/master/Source/rails.js. The accepts header is being set properly to "text/javascript".

推荐答案

这是我要做的事情:(与javascript库无关)

This is what I had to do to get this to work: (and has nothing to do with the javascript library)

# whatever_controller.rb
def index
  respond_to do |format|
    format.js   # renders index.html.haml inside application.js.haml layout
    format.html # renders index.html.haml inside application.html.haml layout
  end
end

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

Alternatively, you could just rename the template for the action to index.js.haml instead of index.html.haml, which would work for ajax requests without using respond_to. However, that would mean that search engines and browsers without javascript enabled would not be able to access the page.

另一种可行的方法是使用respond_with:

Another method that would also work is to use respond_with:

# application_controller.rb
respond_to :html, :js

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

这篇关于Rails没有为JavaScript请求选择正确的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆