Rails Debugging - Exit controller prematurely - ruby-on-rails

I was wondering how to exit a controller in rails and get the output up to that point.
In PHP I often used the "exit" when debugging to get only the data processed to that point. I haven't found a sollution to this in rails.
If you get a error further down in the code the view is locked from displaying <%= debug %> information.
Some would suggest console or rescue, and I know about these. But isn't there a simpler solution?

In development mode, I often just use puts or awesome_print to print something to the screen that I ran rails server from. That works pretty well for the simple cases.
For anything more complex than that, I use ruby-debug or pry to drop down into an interactive console when it hits the right point.
I have some editor shortcuts to print one of these two snippets:
require 'pry'; binding.pry
require 'ruby-debug'; debugger
Drop these in your code and you can use IRB to inspect (and manipulate) the state of your program.
I highly recommend you give pry a shot. Check it out here:
http://pryrepl.org/
http://railscasts.com/episodes/280-pry-with-rails
There is also the older ruby-debug:
http://railscasts.com/episodes/54-debugging-with-ruby-debug

Related

Ruby debug is stepping into rather than stepping over

I am writing a Rails 4 application in Ruby 2, and I am using the debugger gem to debug my code.
Here is the situation currently:
I place a debugger statement in my Rspec tests, run the tests in the shell, and the program breaks as expected. Wishing to step to the next line, I enter n, but the debugger actually steps into the code, showing me the inner workings of libraries I don't care to see.
So the issue is, the debugger command n is acting like s.
How can I solve this problem*?
*I am not willing to put a breakpoint on the next line and then continue, that will get very old, very fast.
Debugger does not yet fully support Ruby 2.0, and one of the issues is that next incorrectly acts like step. You’ll have to wait till the issue is fixed for it to work correctly, or use an alternative debugger such as Byebug.

Debugging in rails 3

i want to debug ROR without going through the effort of putting inspect method for every relevant object in the controller as well in the model.is there a better way as we have in Java (Run time debugger using eclipse).i know that I can Use Rails.logger and also make use of rails Console(irb`).i am even aware of debugging/inspecting elements in erb/rb file.Still is there a better,quick and reliable way to debug a Rails app.
There is much better, see this railscats.
It presents two great gems, especially Better Errors
Otherwise, you could use pry with rails, see this railscast.
you can also use pry-rails, pry-debugger and then use binding.pry method in your code and then while using your app you have Rails console available in rails server
Add this lines to your application's Gemfile
group :development do
gem 'ruby-debug19'
end
then run cammand
bundle install
add debugger within your controller or model method, stop the rails server and restart again. Whenever rails found word debugger it stops control at that point. You can easily debug your value or object.
Hope this will helps you.

rails - hide 'source of your encoding' from terminal

I'm new to ROR and I"m having trouble with all the text that rails spits to the terminal window. Primarily, the html from my web pages get repeated in the terminal window and I'm really just wanting to see the important stuff like sql queries and error messages. I'm wasting a lot of time scrolling throughout the terminal window trying to find what I need b/c of all the HTML that fills up the screen.
Is there an option to disable the 'source of your encoding' output?
Thanks.
You could raise your log level
The rails guide gives a good example
http://guides.rubyonrails.org/debugging_rails_applications.html#log-levels
Make sure if you are in development to change it in config/environments/development.rb
I don't know if this will really solve your problem though because I am unsure of exactly what output you are looking for. A higher log level may throw out the baby with the bathwater.

PRY Gem Issue When Loading Many Records

I am having an issue with using the PRY console for Ruby. When I fetch many records (e.g. Account.all) the output fills the page, and forces me to scroll to the end where I find an (END). However I can't type anything and cannot return to the pry prompt.
If I type a similar command which doesn't return enough records to fill the terminal window, everything acts normally (i.e. Pry prints out the returned records and returns me to the prompt).
Any ideas? I am using Pry version 0.9.10 on Ruby 1.9.3.
Thanks!
Type q so you can type other command
Why?
The only thing that state pry have this feature is in here https://github.com/pry/pry#code-browsing
Code that is longer than a page is sent through a pager (such as
less),
If you are a Mac or Linux user you maybe are familiar with tools like Less or More that helps you see through documents by pagination in the terminal and pry implemented this feature as it's super useful sometimes when there is much things being printed out to the terminal.
To learn what you can do when in pagination mode in pry you should check this out http://www.thegeekstuff.com/2010/02/unix-less-command-10-tips-for-effective-navigation/

Ruby / Rails debugging strategy

Please can you share your approach / methodology to debugging in Ruby / Rails.
I'm busy with the Rails tutorial, and am getting the error:
NoMethodError in UsersController#show
undefined method `microposts' for #<User:0x83b43e8>
And that got me thinking about debugging strategies. Does anyone have advice for a new Rails user (and new MVC user) on strategies to approach debugging. What path do you follow? Is there a generally accepted approach? Is there a way to step through the code?
Right now I am using unit testing as a kind of "lint" checker, but that only goes so far.
Although I want to solve it, the actual error I am getting right now is not the main thrust of this question.
(PS: The problem is not a duplicated "show" as documented in elsewhere on Stackoverflow
I haven't seen this mentioned yet but another option is to put a debugger statement in your code. Like this
def some_method
something = 3
debugger
# ... more code
end
If this is in a rails app when the code reaches debugger it will cause the terminal window running the web server to jump into something that looks like an irb session (I'm not exactly sure what it is). From there you can do puts something to see what the value is for example. You can even puts params to see what all the params values are. You can also step through the code, jump to a specific line, etc.
Pry seems to be a better way to go about this but it's how I used to debug before I knew about pry.
Note: You might need to do require 'ruby-debug' if you're doing this outside of rails.
i use a combination of irb, print statements, logging and pry bindings. (pry is a great gem)
irb is a great way to just play around with your ruby or rails app in the console. You could just enter the code from your controller (or similar) and see if it breaks in console for faster feedback loop. But remember you have to do reload! if you change anything in your class/module.
print statements are easy if you're running tests and just want it to output something a different points in your test. But if your testing in a browser I would recommend writing to the logger: Rails.logger.debug "...". But remember to set your logging level in your configuration to DEBUG -or- you can just do Rails.logger.info instead which should show up by default. Then you can just tail or view the logs in my_app/logs/development.rb.
My favorite method for really tricky bugs is that if the error is happening in a test you can just place binding.pry in the preceding line and then it will pause your test at that line and drop you into a console. I recommend watching the rails casts for more in-depth info: http://railscasts.com/episodes/280-pry-with-rails
I do not start Rails project without 'pry' gem.
Add gem to Genfile:
group :development, :test do
gem 'pry'
end
and stop request execution anywhere in your project, just put
binding.pry
to your model, controller, tests ..., or
<% binding.pry %>
in your view's, templates, partials.
Then you can check what ever you want objects, params, variables ...
Type exit to leave pry environment, and request will continue.
The Ruby on Rails Guide would be a great place to start, but there's plenty more.
I always have a rails console session or at minimum an irb session to play with to see if things do what I think they do.
I also use RubyMine which has an excellent integrated debugger http://www.jetbrains.com/ruby/
Beside pry gem, another option would be byebug. This gem enables you to temporarily stop code execution at a breakpoint, which is marked with keyword byebug inside the code. When execution reaches the breakpoint, a marker will be pointing to the current line, and you are able to type in commands.
This Gem offers a huge set of commands, the most commonly used ones are:
next - this command enables you to go to next line
step - goes into each invoked method step by step
break - it stops the execution of code
continue - continues code execution
This is a great article to check for debugging in rails.

Resources