我在我的應用中有兩種不同的佈局,一種是針對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