2

我正在使用 Rails 4.2。我使用工头启动 Rails,我想在启动 Rails 时附加一个调试器。这个问题详细介绍了 Rails 3.2 的过程——如何调试由工头启动的 rails (3.2) 应用程序?,但是我相信这个文件已经过时了

$ cat config/initializers/start_debugger.rb
# Enabled debugger with foreman, see https://github.com/ddollar/foreman/issues/58
if Rails.env.development?
  require 'debugger'
  Debugger.wait_connection = true

  def find_available_port
    server = TCPServer.new(nil, 0)
    server.addr[1]
  ensure
    server.close if server
  end

  port = find_available_port
  puts "Remote debugger on port #{port}"
  Debugger.start_remote(nil, port)
end

因为我不认为 Rails 4.2 支持“调试器”gem。如何在使用 Rails 4.2 的专用调试端口上使用工头启动 Rails?

4

1 回答 1

0

根据您的评论,您说您可以使用 byebug,所以这是为 Foreman 设置它的指南

编辑 config/environments/development.rb

require "byebug/core"
port = ENV['DEBUGGER_PORT'] # any port that you like
Byebug.wait_connection = true
Byebug.start_server("localhost", port)

假设你想调试你的控制器

def index
  @posts = Post.all
end

并且您想查看@posts价值然后只需在之后添加 byebug@posts

def index
  @posts = Post.all
  byebug
end

并且对您的#index端点rails服务器的请求将在byebug您需要byebug -R localhost:8899在另一个终端中执行的线上暂停


如果你看到这个https://github.com/deivid-rodriguez/byebug/blob/master/lib/byebug/remote/server.rb#L30。它也使用 TCPServer。所以它类似于你自己的实现。

您的工头设置也可以读取 ENV['DEBUGGER_PORT']

于 2021-10-08T14:15:37.067 回答