我在我的应用中有两种不同的布局,一种是针对javascript(AJAX)请求,另一种针对常规请求。Rails没有为javascript请求选择正确的布局
# 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 "https://stackoverflow.com/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 "https://stackoverflow.com/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成功处理。
我在这里有点难住。我希望我犯了一个简单的错误,有人可以指出!
谢谢!
P.S.我正在运行rails 3.2.1(以前在3.1.3上试过同样的问题)
编辑:不知道它是否有所作为,但我使用的是mootools-rails驱动程序:https://github.com/kevinvaldek/mootools-ujs/blob/master/Source/rails.js。接受标题正确设置为“text/javascript”。
谢谢,我可以尝试,但不应轨根据请求标题自动选择合适的布局?请求标头当前设置为“text/javascript”(使用mootools rails驱动程序),但它使用错误的布局。另外我在控制器的顶部添加了respond_to:js,但这也没有帮助。 – mattwindwer 2012-01-27 20:18:22
另外,如果用户没有启用javascript,或者对于搜索引擎,我希望application.html.haml作为后备的布局 – mattwindwer 2012-01-27 23:42:00