I am new to ruby on rails, I want to debug my rails application through debugger i rails server console.So please tell me shortcuts and there meaning so that I can be able to debug.
In order to open the rails console in production, you can type:
RAILS_ENV=production bundle exec rails c
Or, if you want to debug code during runtime, you can use:
pry-remote
Rails 4 contains by default byebug and web-console. In development mode you can simply call byebug anywhere in the code to stop execution and get a debugger console.
In views you can use:
<%= console %>
to access an IRB console on exception pages.
See debugging guide.
If you don't have Rails 4, add to your Gemfile following:
group :development, :test do
gem 'byebug'
end
and run bundle install.
The official way to do this is using the byebug gem. Install it by executing
gem install byebug
After that you can put a byebug statement anywhere in your code:
class PeopleController < ApplicationController
def new
byebug
#person = Person.new
end
end
As soon as the application reaches this statement, it stops and presents a command prompt in the shell you started the server process.
See the RoR Debugging Guide for further details.
Related
I recently installed pry to replace my irb. It was working fine at first, but now every time I run pry it doesn't recognize the local app environment at all. I get something like the following:
[3] pry(main)> show-models
NameError: undefined local variable or method `show' for main:Object
from (pry):2:in `__pry__'
I've tried uninstalling and reinstalling the pry-rails gem and I've added the following code to an initializer file:
Rails.application.configure do
# Use Pry instead of IRB
silence_warnings do
begin
require 'pry'
IRB = Pry
rescue LoadError
end
end
end
Any thoughts on what this could be? I can't seem to find any info on this.
My gem file looks like this:
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
gem 'pry-rails'
gem 'pry-byebug'
end
You probably run Pry using pry command, when you should be using rails console command (be sure that you run it from your Rails app directory). If that doesn't work for you, then try bin/rails console and bundle exec rails console commands.
I also Use pry
I'm also using pry and not seen command like show-models yet. It doesn't work for me too. And I never use such command. Using Model.all working fine.I don't if commands you are using is feature of pry. If so visit its documentation. I think its problem due to you gave wrong command.
I had some different problems using pry and byebug together.
i deleted byebug and all is well.
Hope it will also solve ur problem.
I use gem 'pry' in my gemfile instead of gem 'pry-rails' though
Not sure what it was, but a system restart did the trick. Problem solved.
Sometimes I have reason to want to start the rails console as an irb repl rather than pry (as awesome as pry is). It will default to pry because pry has in the Gemfile. Hows is that done nowadays?
I think there used to be a --irb option when running rails console but that seems to be gone now. I get a deprecation error message when I try it.
More details
If I just run "rails console" it takes me to pry.
If I run "rails console -irb=irb":
$ rails c -irb=irb
--irb option is no longer supported. Invoke `/your/choice/of/ruby script/rails console` instead
Relevent lines from my Gemfile:
gem 'rails', '3.2.18'
gem 'pry-rails'
gem 'pry-plus'
Launching pry when calling rails console or rails c is set up by the pry-rails gem. If you look in the pry-rails issues there is one that describes a solution.
Define the environment variable DISABLE_PRY_RAILS as 1.
So you can call rails console without pry with:
DISABLE_PRY_RAILS=1 rails c
Works in Rails 4: In your application.rb, inside your Application class, drop this puppy in.
# Use the IRB console instead of the Pry one
console do
require 'irb'
config.console = IRB
end
I couldn't take the Pry console anymore. It kept putting my cursor in odd places at unpredictable times. I can't even describe it but if you know what I'm talking about and know the solution, please let me know.
Inspired by the answers above, I added the following to the class definition in application.rb so that Pry is toggleable from the console:
console do
if ENV['IRB']
require 'irb'
config.console = IRB
end
end
You can then run rails c to get a Pry console, and IRB=true rails c to get an IRB console. This is easily modified if you want the inverse. Works in Rails 4 and 5.
For the benefit of anyone who runs into the same problem, this is my (crappy) workaround.
I wrapped the pry gems in Gemfile with this:
...
unless ENV['NOPRY']
gem 'pry-rails'
gem 'pry-plus'
end
...
Then run this from the unix terminal:
NOPRY=true bundle install
NOPRY=true rails console
Not pretty, but gets the job done...
You can also do it once console has already been started via IRB.start method.
I recently found pry and I find it to be a great replacement for irb. I figured I'd use it as replacement for my ROR development and debugging.
I know that to open pry with a rails app you simply type
pry -r ./config/environment
My question is that is there a way to open the pry console in a sandbox mode so that any modification I make does not affect my database.
Firstly add "pry-rails" gem into Gemfile
gem 'pry-rails', :group => :development
Then bundle install
Then launch rails console in sandbox mode
# in development env
$ rails c --sandbox
# or in test env
$ rails c test --sandbox
That's all. Pry will replace irb automatially. Enjoy!
Ref: https://github.com/pry/pry/wiki/Setting-up-Rails-or-Heroku-to-use-Pry#
If you don't want to modify your Gemfile, you can do this once you open pry:
require 'active_record/railties/console_sandbox'
I have this defined in a method in my ~/.pryrc so it's easy to use.
I installed debugger gem in my gemset
<pre>
nikhil#nikhil-K54C:/media/media1/railscode/beasport$ bundle show debugger
/home/nikhil/.rvm/gems/ruby-1.9.3-p194#beasport/gems/debugger-1.2.0
</pre>
My intention is to use this gem to debug my application by placing "debugger" call in my code so that i can use it to debug application.
However strangely even if i have not put "debugger" call anywhere in my code but still whenever i hit any url to my application (from localhost:3000), debugger starts debugging in file
<pre>
/home/nikhil/.rvm/gems/ruby-1.9.3-p194#beasport/gems/actionpack-3.2.8/lib/action_controller/metal/implicit_render.rb
</pre>
here is what i get when i do "list"
<pre>
=> Ctrl-C to shutdown server
[2012-11-07 19:11:18] INFO WEBrick 1.3.1
[2012-11-07 19:11:18] INFO ruby 1.9.3 (2012-04-20) [i686-linux]
[2012-11-07 19:11:18] INFO WEBrick::HTTPServer#start: pid=6602 port=3000
/home/nikhil/.rvm/gems/ruby-1.9.3-p194#beasport/gems/actionpack-3.2.8/lib/action_controller/metal/implicit_render.rb:5
default_render unless response_body
(rdb:1) list
[0, 9] in /home/nikhil/.rvm/gems/ruby-1.9.3-p194#beasport/gems/actionpack-3.2.8/lib/action_controller/metal/implicit_render.rb
1 module ActionController
2 module ImplicitRender
3 def send_action(method, *args)
4 ret = super
=> 5 default_render unless response_body
6 ret
7 end
8
9 def default_render(*args)
(rdb:1)
</pre>
I am using webrick. This is frustrating. When i remove debugger gem from gemfile and do "bundle install" then it works fine but i wont be able to debug my application then.
Again i checked with my other application and there debugger works just fine and do not go to implicit.rb unnecessarily.
Please help
I know this is a question posted last year
and you may no longer need answer, but since I also had the same issue and had to solve it myself, I'm posting what worked for me.
gem uninstall debugger
gem uninstall debugger-linecache
gem uninstall debugger-ruby_core_source
Then,
gem install debugger
This is my first time trying to get a Gem to work in rails where I haven't just been able to follow the documentation.
I first installed the gem using sudo gem install ping back and then added it to my Gemfile via gem 'pingback'. I then ran bundle install and it shows it installed in the list it outputs.
So then I wrong a little function that looks like this and is in my posts controller:
def send_trackback(posts)
posts.each do |post|
source_uri = "http://example.com/posts/#{post.slug_url}"
target_uri = post.target_url
Pingback::Client.new.ping(source_uri, target_uri)
end
end
whenever I try to load the admin page that sends the trackbacks I get the following:
NameError in PostsController#pingback
uninitialized constant PostsController::Pingback
Do I have to do more than just install the gem via bundler and then plug and play?
Update
adding require 'pingback' to the top of my posts controller results in this:
cannot load such file -- pingback
The error message to me indicates that the VM is trying to find PingBack in PostsController, I am thinking you are missing a require or include statement for PingBack.
It may be a typo, but pingback needs to be one word, not 'ping back' for the line in the gemfile, and for the gem install.
I would try running 'bundle list' to make sure the gem is installed.
I restarted the rails server and I believe this has resolved this issue.