7

現在、erbテンプレート(HTTPServer / cgiで使用するため)でエラーが発生した場合、次のようにします。

  • 小さな変更の場合は、元に戻して保存し、再テストします。
  • 大きな変更または新しいファイルの場合は、コードの1/2を削除またはコメントして、再テストします。壊れたコードを削除/見つけるまで、バイナリ検索を実行します。

呼び出しスタックは、私の.rhtmlファイルの何にも対応していないようです。

(erb):6:in `block in <main>'
/opt/local/lib/ruby/1.9.1/erb.rb:753:in `eval'
/opt/local/lib/ruby/1.9.1/erb.rb:753:in `result'
bin/correct.rb:45:in `block in <main>'
/opt/local/lib/ruby/1.9.1/webrick/httpservlet/prochandler.rb:26:in `call'
4

4 に答える 4

8

これがこの問題に当てはまるかどうかはわかりませんが、誰かに役立つかもしれません。私はレール5を使用しています。

    <% debugger %>

html.erbファイルでは、Railsサーバーが実行されているターミナルウィンドウが一時停止します。そこから、html.erbファイルにあるパラメータや変数をデバッグできます。

于 2017-06-15T17:44:55.307 に答える
5

ダニエルが言ったように、ほとんどの場合、エラーメッセージはエラーがどこにあるかをすばやく見つけるのに役立ちます。

確かにそうでない場合もあります。

そのバイナリ検索を行うための面倒で高速な方法は、次のような間違った行を挿入することです。

<%= the_error_is_after_this_line %>

次に、正確な行が見つかるまで行を移動します。

私は、時間ごとに大量の行を書くことができ、うまく機能する優秀なプログラマーの1人ではありません。私は通常、小さなステップで開発し、毎回ブラウザにページをリロードします。

とはいえ、デバッグが難しいビュー(またはメソッドなど)を回避するためのより良い方法は、単純で短いビューを作成することです。私の大まかなルールは、単なるhtmlでない限り、エディターウィンドウでビュー(またはメソッド)全体を読み取ることができなければならないということです。

常にヘルパーと部分ビューを使用してください。erbビューの行で2つ以上()または[]を数えることができますか?はいの場合は、ヘルパーを使用してください。

ビューで2つまたは3つ以上のブロックを数えることができますか?いくつかのパーシャルを使用してください。

于 2009-08-29T06:33:50.117 に答える
0

一般に、Erbエラーはそれらが発生した場所を示します。たとえば、ここでのエラーはerbファイルの6行目にあります。バックトレースに付属のエラーメッセージを省略しましたが、通常、検索するエラーの種類がわかります。たとえば、ここでの私の簡単なテストでは、次のようになります。

NameError: undefined local variable or method `asdf' for main:Object
  from (erb):7
  from (irb):6

何がどこで間違っているのかは十分に明らかです。

エラーとその原因となったerbに関する詳細情報を投稿できますか?

于 2009-08-29T05:47:16.667 に答える
0

Rails 5では、デフォルトでGemfileでgem'byebug'を見つけることができます。

    group :development, :test do
      # Call 'byebug' anywhere in the code to stop execution and get a debugger console
      gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
    end

次に、コントローラーでbyebugを使用し、必要な場所に何度も配置して、「ブレークポイント」のように機能し、最終的にサーバーを実行できます。$ rails server

class UsersController < ApplicationController
   byebug
end

コマンドラインでオプションのヘルプを記述します。通常、文字「c」を使用して次のブレークポイントに進むか、文字「n」を使用して段階的に進み、ctrl+dを使用して終了します。

 (byebug) help

  break      -- Sets breakpoints in the source code
  catch      -- Handles exception catchpoints
  condition  -- Sets conditions on breakpoints
  continue   -- Runs until program ends, hits a breakpoint or reaches a line
  debug      -- Spawns a subdebugger
  delete     -- Deletes breakpoints
  disable    -- Disables breakpoints or displays
  display    -- Evaluates expressions every time the debugger stops
  down       -- Moves to a lower frame in the stack trace
  edit       -- Edits source files
  enable     -- Enables breakpoints or displays
  finish     -- Runs the program until frame returns
  frame      -- Moves to a frame in the call stack
  help       -- Helps you using byebug
  history    -- Shows byebug's history of commands
  info       -- Shows several informations about the program being debugged
  interrupt  -- Interrupts the program
  irb        -- Starts an IRB session
  kill       -- Sends a signal to the current process
  list       -- Lists lines of source code
  method     -- Shows methods of an object, class or module
  next       -- Runs one or more lines of code
  pry        -- Starts a Pry session
  quit       -- Exits byebug
  restart    -- Restarts the debugged program
  save       -- Saves current byebug session to a file
  set        -- Modifies byebug settings
  show       -- Shows byebug settings
  skip       -- Runs until the next breakpoint as long as it is different from the current one
  source     -- Restores a previously saved byebug session
  step       -- Steps into blocks or methods one or more times
  thread     -- Commands to manipulate threads
  tracevar   -- Enables tracing of a global variable
  undisplay  -- Stops displaying all or some expressions when program stops
  untracevar -- Stops tracing a global variable
  up         -- Moves to a higher frame in the stack trace
  var        -- Shows variables and its values
  where      -- Displays the backtrace

(byebug)

debug(params)を表示するその他のオプション:app / views / layouts / application.html.erbファイルのレンダリングフッターの上に、次のファイルを配置します。

<%= debug(params) if Rails.env.development? %>

最後に、Ruby on Railsの初心者として知っているように、このオプションを共有します。お役に立てれば。

いくつかのヘルプのソース:https ://rubyplus.com/articles/3631-Debugging-using-ByeBug-Gem-in-Rails-5

于 2019-06-11T02:05:04.570 に答える