5
由於許多天,我試圖理解爲什麼這樣一個簡單的鏈接:application.haml和application.html.haml之間的區別?
link_to 'My Link', my_path(format: :js), remote: true
總返回,而不是執行JavaScript位於我file.js.erb完整的HTML文檔:
alert('hello world')
[...]
後調試的時間,我發現了原因:
當我重新命名我的主要佈局文件如:application.haml
它呈現完整的HTML文檔:
Started GET "/my_path/2.js" for 127.0.0.1 at 2016-03-05 12:28:20 +0100
Processing by MyController#show as JS
Rendered my_path/show.js.erb within layouts/application (0.1ms)
Rendered layouts/_sidebar.html.erb (18.9ms)
Rendered layouts/_headbar.haml (0.5ms)
Rendered layouts/_flash_messages.html.haml (0.2ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 102ms (Views: 59.3ms | ActiveRecord: 2.9ms)
當我如重命名我的主要佈局文件:application.html.haml
它正確地執行JavaScript和運行我的的Hello World彈出:
Started GET "/my_path/8.js" for 127.0.0.1 at 2016-03-05 12:28:34 +0100
Processing by MyController#show as JS
Rendered my_path/show.js.erb (0.1ms)
Completed 200 OK in 24ms (Views: 21.8ms | ActiveRecord: 0.4ms)
根據我的佈局的不同文件名,爲什麼JavaScript行爲有差異?
這不是JavaScript行爲,它是如何搜索模板。首先搜索'。<模板引擎>'文件,然後搜索'。 <模板引擎>'。因此,如果您的通用模板沒有格式,則會採用所有格式。 –
BroiSatse
如果其中一個視圖模板缺失,軌道應報告錯誤,告訴它如何搜索它,包括路徑,格式等。使用這些信息,您將知道搜索順序。 – canoe