我正在尝试使用 pry-byebug 工具调试我的代码,但是当我在终端中启动“rails 控制台”然后调用调试器应该在其中起作用的类方法时,什么也没有发生。我已经安装了 gem 并按照以下文档进行了捆绑安装: https ://github.com/deivid-rodriguez/pry-byebug
所以基本上我有 2 个模型,Grid 和 Cell。创建 Grid 实例时,我想创建相应的 Cell 实例。到目前为止它不起作用,但我的主要问题是我无法调试并知道哪里出了问题。
因此,我在 Grid 类的 create 方法中添加了 binding.pry 命令。希望逐行浏览其余代码并了解我在哪里做错了。
但是,当在“rails 控制台”中执行类似 Grid.new(session_id: 1, rows: 3, columns: 3) 的操作时,我所看到的只是实例已创建并保存到数据库中。
所以我管理访问方法“create”但没有显示 put 也没有得到 pry-byebug 选项。
甚至我的日志也没有显示任何错误消息。
我已经阅读了有关该主题的其他帖子,但到目前为止没有一个对我有用,例如: Breakpoints with pry-byebug don't trigger in the console
这是我的 GridController
require "pry-byebug"
class GridsController < ApplicationController
# after_create :generate_cells_for_grid
after_action :generate_cells_for_grid, only: [:create]
def new
@grid = Grid.new
end
def create
puts "In the CREATE method" # This puts never shows up in the console
binding.pry
@grid = Grid.new()
@grid.save
end
def generate_cells_for_grid
rows = @grid.rows
columns = @grid.columns
id = @grid.id
# Creating cells for every position to fill the grid
for row in 1..rows do
puts row
for column in 1..columns do
puts row
puts column
Cell.create({
grid_id: 1,
player_id: 1,
colour_id: 1,
x_coordonate: row,
y_coordonate: column,
alive: false
})
end
end
end
def index
end
def show
@grid = Grid.find(params[:id])
end
def update
@grid = Grid.find(params[:id])
@grid.update(params[:grid])
end
def edit
end
def delete
end
end
这就是我在“rails 控制台”中得到的:
[30] pry(main)> Grid.create(session_id: 1, rows: 3, columns: 3)
(1.7ms) BEGIN
Session Load (0.3ms) SELECT "sessions".* FROM "sessions" WHERE "sessions"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Grid Create (0.7ms) INSERT INTO "grids" ("session_id", "rows", "columns", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["session_id", 1], ["rows", 3], ["columns", 3], ["created_at", "2019-11-07 11:07:08.951668"], ["updated_at", "2019-11-07 11:07:08.951668"]]
(0.5ms) COMMIT
=> #<Grid:0x00007fce6b2cf790
id: 31,
session_id: 1,
rows: 3,
columns: 3,
created_at: Thu, 07 Nov 2019 11:07:08 UTC +00:00,
updated_at: Thu, 07 Nov 2019 11:07:08 UTC +00:00>
我做错了什么没有激活 pry-byebug?有没有更有效的方法在 Rails 上进行调试?
谢谢!