Debugging/Breakpoint-ing the Rails Core with Ruby-Debug? - ruby-on-rails

How do I debug the rails gems? I've just found the beauty of ruby-debug and am real excited, but I want to step through the routing code in action_controller, but the debugger isn't set up at this time, it seems like it's just skipping over my "debugger" calls in action_controller/routing/route_set.rb for example.
What am I missing?

I just tested this with Rails 2.3.4. I added a 'debugger' line to the call method in vendor/rails/actionpack/lib/action_controller/routing/route_set.rb, ran 'rdebug script/server', browsed to a page, and it stopped at the correct line.
You can also use a class/method breakpoint; you'll need to step through the first few lines of the app until you're past the require 'action_controller' line, and then enter: b ActionController::Routing::RouteSet.call.
Or you can try setting a breakpoint based on the file name and line number.

Related

Starting Byebug with a block

I'm wondering if it's possible to start a Byebug session giving a starting point from a Rails console. I know I can insert a byebug statement wherever I want and start debugging, but I'd like to do something like this:
Byebug.start do
# entry point
User.find(12).problematic_method
end
Thanks.
I opened the class and override the problematic_method inside the Rails console and added the byebug statement where I wanted it. This way I don't have to change the running production code (I forgot to mention above I want to debug in production).
This workaround will be enough for my purposes. The only problem is that you don't have the debug code listing available for that method, but its fine.
That is not possible. What you can do, is write your code inside a .rb file and debug that file/script using byebug.

Debugging with Rails

is a very beginner question but even tho i cannot find any helpful documentation online.
I'm working on Discourse application which is Ruby on Rails + Ember based.
I need to print out in my terminal (not in my view!) the value of a variable.
I try:
debugger
put varName
logger.debug varName
but none of them print anything in my console.
What i need to do is stop my console from running and print a value.
Like debugger; would do in javascript.
How can i do this simple task?
1) not sure if this change anything but the .rb file i try to debug is a "services".
2) yes, i read about pry, i installed it and add to my file:
require 'pry'
binding.pry
But i don't see anything happening in the console.

How to extend the String class in a command line app?

I primarily work in Rails and I'm using a command line data conversion gem, "Mongify" and I am stumped about how to extend core classes in a Ruby cli app.
I want to extend the String class with an .is_date? method to check whether a string can be converted to a Date. I've got it working in the Rails Console,
I added a string.rb file to lib/ext with the following;
class String
def is_date?
begin
return true if Date.parse(self)
rescue
#do nothing
end
return false
end
end
Then in a Rails console I do a require 'ext/string' and it will work.
But I can't figure out how to get it to work in the Mongify cli app. I copied string.rb into the lib folder of the gem and I've tried adding require 'string' to a number of different files in the gem, but I keep getting undefined method errors.
Can someone point me in the right direction?
How about you require it from lib/mongify.rb like so:
require 'string/extensions.rb'
And then put your code in lib/string/extensions.rb
Let us know the exact undefined method errors you're getting in case this isn't the solution.
To help you with the debugging exercise that would give you the answer you need. Start by putting a breakpoint right before the place of the function call.
In the debugger, load the required document and then step past your breakpoint to the next one after the call has occurred.
Once you have this working, then start earlier in the stack trace – in a file that loaded before that one. Keep moving backwards until you get to a sufficiently early part in the load process of the gem, and make that be the place you load your code.

Delayed_job won't make any changes to database

I am having many problems running DJ. Primarily, I cannot get delayed_job running any methods that change the database. I am testing locally with a sqlite3 database, DJ 3.0.0, and I even added the delayed_job_active_record gem.
I have, for example, tried to run the following method in the background:
#user = User.find(1)
#user.delay.recorder_method
Where this method is:
def recorder_method
self.relevant_field +=1
update
end
This creates a delayed job, and the handler has the appropriate info for the user and the appropriate method name. The script runs the job, thinks it has succeeded and thus deletes the record from the delayed_job table. BUT the user is unchanged (the database is unchanged).
What on earth is going wrong? Note that, when I run the same code ("#user.delay.recorder_method") from rails console, it works.. and the difference is the handler created by the rails console call is:
--- !ruby/object:Delayed::PerformableMethod
object: !ruby/ActiveRecord:User
attributes:
... (attribute info and rest of file)
Whereas the one created in by the call in a controller action is:
--- !ruby/struct:Delayed::PerformableMethod
object: !ruby/ActiveRecord:User
attributes:
Not the difference in line 1 of both things (ruby/object vs ruby/struct.. the former works and the latter doesn't). Maybe this is something that might signal what is going wrong. Any ideas, anybody?
Your Rails app is running an old version of the DJ gem and your console is running a new one. About six months ago, Delayed::PerformableMethod was refactored from a Struct into a regular class: https://github.com/collectiveidea/delayed_job/commit/7b8a79a72c0ee5d8bac4bc0b183d1cce9cedff85 (So your Rails app is running a gem at least six months old and your console is running one newer than that).
This is an easy fix. First update the appropriate line in your Rails Gemfile. You'll see a line like this:
gem 'delayed_job_active_record'
If it has a version specification, make sure it's updated to the newest. Then from the command line (in the Rails root), run:
bundle update delayed_job_active_record
For anyone interested, removing the following lines from boot.rb solved this issue for me:
require 'yaml'
YAML::ENGINE.yamler = 'syck'

Missing helper file error after Rails3 conversion

I'm in the midst of converting an old Rails2.3 project to 3, and I'm running into this runtime error when I load the first page:
Missing helper file helpers/activesupport.rb
Full stacktrace here
Has anyone else run into this? Looks like something changed in how helpers are loaded, but I don't see any obvious solutions.
I was able to work around the problem by created an empty file at app/helpers/activesupport.rb but I would like to know why this is happening in the first place.
Could it be a clash with ActiveSupport?
I am not sure why its even looking for such a helper - do you have a model or controller called activesupport?
I was having a similar issue with Hpricot. I had a require 'hpricot' statement in a helper, but I didn't have Hpricot in my Gemfile.
In your case, if you were explicitly requiring ActiveSupport somewhere, you would have to add it to your Gemfile (I just tried it and despite having Rails in my Gemfile, I still got the same error you were getting).

Resources