I am trying to debug my code using the pry-byebug tool, but when I launch "rails console" in my terminal and then call on the class method in which the debugger should come into action, nothing happens. I have installed the gem and did a bundle install as per the documentation on: https://github.com/deivid-rodriguez/pry-byebug
So basically I have 2 models, Grid and Cell. When a Grid instance is created I want to create the corresponding Cell instances. So far it doesn't work, but my main problem is that I am not able to debug and know where things go wrong.
I have therefore added the binding.pry command in the create method of the Grid class. Hoping to go through the rest of the code line by line and understand where I did something wrong.
However when in "rails console" I do something like Grid.new(session_id: 1, rows: 3, columns: 3), all I see is that the instance has been created and saved to the DB.
So I manage accessing the method "create" but not showing either the puts nor getting the pry-byebug options.
Even my logs don't show any error message.
I have read other posts on that subject but none have worked for me so far, such as this one: Breakpoints with pry-byebug don't trigger in the console
Here is my 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
This is what I get in the "rails console":
[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>
What am I doing wrong to not activate the pry-byebug? Is there a more efficient way to debug on Rails?
Thanks!