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.
Related
I'm debugging a Rails application that uses gems from my local environment. Is there a way to place a breakpoint in one of the gem's file and have it trigger when I debug my app ?
How can I see the source code of a gem installed on my machine? should help you
If you need to edit the source code of say Devise, you would run:
bundle open devise
This will open the gem in your default text editor, allowing you to put breakpoints wherever needed.
Alternatively to see where it's stored on your machine try:
bundle show devise
You can use gem 'pry-rescue' and gem 'pry-rails' in your gemfile to debbug your code. Just put in your group development like this:
group :development do
gem 'pry-rails'
gem 'pry-rescue'
end
in your code you can put
binding.pry
and after that just run your application and your request will stop where you put the binding.pry
I just started a new project with RAils 4.2 on ruby 2.1.2. In the Gemfile, it clearly states that pry-byebug is included, and in my ~/.pryrc file I have the following
puts "HELLO!!"
if defined?(Rails) && Rails.env
extend Rails::ConsoleMethods
end
require 'hirb'
Hirb.enable
When I do
$ rails c
I don't see "hellO' being printed and I don't get Hirb's pretty format
Any chance someone can tell me where should I start looking where the error might be?
Thank you!
I had to include gem 'pry-rails' in my Gemfile, and not within any groups.
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.
included 'pry-rails' in my Gemfile run console and get the following
#marketplace-rails/gems/pry-rails-0.2.0/lib/pry-rails/commands.rb:3:in `block in <module:PryRails>': undefined method `create_command' for #<Pry::CommandSet:0x007fcfaa67a878> (NoMethodError)
Step 1. don't use 'pry-rails' ? (not sure how to get that gem
working)
Step 2. in gemfile include gem 'pry'
Step 3. in application.rb include the following...
console do
require "pry"
config.console = Pry
end
run rails console be happy to have pry back in your life.
For what it's worth, I just tried this:
group :development, :test do
gem 'pry-rails'
end
Followed by a bundle install and a 'rails c' and pry came up.
Try stopping spring,
$ bin/spring stop
worked for me after installing the gem, didn't even have to change it in the config/environment/development file either
Make sure you have pry included in your Gemfile and add
config.console = Pry
to your config/environments/development.rb or to your config/application.rb if you want it in every environment.
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.