2012-01-27 54 views
0

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

回答

0

這是我必須做的就是這個工作:(並沒有任何與JavaScript庫做)

# 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,該模板適用於不使用respond_to的ajax請求。但是,這意味着沒有啓用javascript的搜索引擎和瀏覽器將無法訪問該頁面。

另一種方法,也將工作就是用respond_with

# application_controller.rb 
respond_to :html, :js 

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

至於佈局,你應該在控制器上明確指定它。也許你可以把名字application.js.coffee改成別的名字,並在控制器的頂部添加layout'new_name'。

+0

謝謝,我可以嘗試,但不應軌根據請求標題自動選擇合適的佈局?請求標頭當前設置爲「text/javascript」(使用mootools rails驅動程序),但它使用錯誤的佈局。另外我在控制器的頂部添加了respond_to:js,但這也沒有幫助。 – mattwindwer 2012-01-27 20:18:22

+0

另外,如果用戶沒有啓用javascript,或者對於搜索引擎,我希望application.html.haml作爲後備的佈局 – mattwindwer 2012-01-27 23:42:00

0

如果你正在使用jQuery UJS然後嘗試加入

data-type="script" 

屬性的鏈接標籤。這將設置jQuery請求的dataType,然後設置您的http請求的Accepts:標頭。 Rails使用它來猜測您的請求的適當響應類型。

看到這個博客帖子的一些背景資料:http://www.alfajango.com/blog/rails-3-remote-links-and-forms-data-type-with-jquery/

+0

好點,但實際上我使用的是Mootools UJS驅動程序(https:/ /github.com/kevinvaldek/mootools-ujs/blob/master/Source/rails.js) – mattwindwer 2012-01-27 19:52:43

2

我有同樣的問題,我試圖用一個新的rails 3.2.7從無到有,爲我所用application.haml,而不是應用.html.erb,遠程鏈接無法正常工作。我想這項工作圍繞:

  1. 轉換application.haml到局部,重命名application.haml_application.haml
  2. application.html.erb只有一條線路:

    <%= render 'application' %>