第三章 RJS 與 Rails
與Rails內的其它東西一樣,RJS也被很好地整合到了框架內。在這一節我們將看看RJS模板的其余部分如何使用在Rails內。
Like everything else in Rails, RJS is well integrated into the framework. In this section we'll take a look at how RJS templates fit in with the rest of the Rails.
第一節 調試
軟件可能沒有bug嗎?幸運地,Rails提供一些內建機制來幫助你定位問題並讓你快速地解決bug。
What would software be without bugs? Fortunately, Rails provides a few built in mechanisms to help you locate the source of your problems and get your bugs resolved quickly.
一、Development Mode Debugging – 開發模式下的調試
缺省地,在開發模式下,所有的RJS調用都被包裝在 JavaScript 的try/catch 塊內。該 try/catch 塊捕獲所有在RJS JavaScript 應答運行期間發生的異常。當異常發生時,有關什麼異常的細節被在彈出的二個警告對話框內表現出來。異常本身的細節在第一個警告對話框內。第二個警告對話框顯示生成異常的代碼。
By default, in development mode all RJS calls are wrapped in Javascript try/catch blocks. The TRy/catch blocks catch all exceptions that occur during the execution of the RJS JavaScript response. When exceptions do occur, details about what went wrong are presented on a serIEs of two alert boxes. The exception itself is detailed on the first alert box. The second alert box shows the code that generated the exception.
調用是由 environment/development.rb 文件內的配置參數config.action_vIEw.debug_rJS 控制的。如果你想在你的開發環境內無效調試,設置這個參數為 false 並重啟動你的web服務器。
Debugging is controlled by the configuration parameter config.action_vIEw.debug_rJS in environments/development.rb. If you'd like to disable debugging in your development environment, set this parameter to false and restart your development web server.
二、Monitoring the Logfile – 監控日志文件
如果Rails在處理一個AJax請求期間引發了一個未處理的異常,應答將會是一個Html錯誤頁面而不是你真的想由 JavaScript 生成的代碼。調試這些問題的方式是監控日志文件。監控你的日志文件的最簡單方式是使用 Unix 或Linux 的 tail 命令。從你工程的根目錄內運行該命令來監控日志文件。
If Rails raises an unhandled exception while processing an
cody> tail -f log/development.log
-f 標志告訴 tail 輸出
The -f flag tells tail to output appended data as the file grows.