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.
Related
Why does my spring gem load in the wrong (or all) environment(s)?
I have this in my Gemfile and spring gem is not listed anywhere else in the file:
group :development do
gem 'listen', '~> 3.1.5'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
When I ran bundle exec rails console test (for the test environment), spring processes started and the Listen module was loaded in the rails console. I made sure all spring processes were stopped beforehand.
To do a sanity check, I removed the whole development group above and bundled. Spring and listen gems were no longer loaded, as I expected.
I faced this misunderstanding in production.
Here's how I solved it:
You can also fix this issue permanently by upspringing (removing the spring gem from) your bin/ executables:
bin/spring binstub --remove --all
Or
spring binstub --remove --all
You can now run the command below to get into rails console in production
rails c --environment=production
Also, to avoid this from occurring in subsequent occasions endeavour to make sure that the spring gem is only present in development and test groups in your Gemfile.
Moreso, when you're in production make sure you always provide the --without development test argument to the bundle install command
bundle install --without development test
and not the usual or common
bundle install
Please note: As an indication, whenever you run the command rails c or rails console and you see the output below:
Running via Spring preloader in process 26651
WARNING: Spring is running in production. To fix this make sure the spring gem is only present in development and test groups in your Gemfile and make sure you always use bundle install --without development test in production
It's an indication that the spring gem is running in your production environment, and it should be stopped or removed entirely from your bin executables.
That's all.
I hope this helps
Spring is generally used through binstubs - did you install the binstubs? If so this is the file your rails command is running through.
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
end
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
As you can see it will load spring anytime you use the rails command. There is no check for the environment. If you do not want to load spring you can use DISABLE_SPRING=1 rails c test.
According the spring gem github page, it looks like rails console will load up spring:
rails console, rails generate, rails runner
These execute the rails command you already know and love. If you run
a different sub command (e.g. rails server) then spring will
automatically pass it through to the underlying rails executable
(without the speed-up).
Also, this is worrying:
You must not install Spring on your production environment. To
prevent it from being installed, provide the --without development
test argument to the bundle install command
rails console (development) would make sense, but not rails console test (or another environment). It seems buggy to me, and a reason now why I don't like the gem.
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.
I have the puma gem in my Rails 4.1.8 Gemfile because I want to use it as the default webserver in all environments. That works fine.
# Gemfile
gem "puma"
In development I am using mailcatcher which means that I'd like to include it as a dependency in my Gemfile.
# Gemfile
group :development do
gem "mailcatcher"
end
This causes the default webserver to be set to thin. This appears to be an unintended consequence of mailcatcher, but it brings up a specific question. Can I create a group that bundler obeys to install Gems, but that Rails ignores? I tried something like this but Rails is still loading the contained gems.
# Gemfile
group :mailcatcher do
gem "mailcatcher"
end
You can alias rails s command... For example export alias rs="rails server puma" and now rs will start puma. This way you are free to use any webserver you want )
If you want to make it really default you can add something like this
require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler.get(:puma)
to the rails script.
I don't know if this will work or not, but what about:
gem "mailcatcher", :require => false
Ref: Bundler: What does :require => false in a Gemfile mean?
Mailcatcher specifically states to not put it in your Gemfile.
Please don't put mailcatcher into your Gemfile. It will conflict with your applications gems at some point.
Instead, pop a note in your README stating you use mailcatcher. Simply run gem install mailcatcher then mailcatcher to get started.
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'm bewildered here. While I can run require "gmail" in irb and successfully load the Gmail gem, doing so in my rails console returns false. I made sure to include all the directories in $LOAD_PATH for irb in the $LOAD_PATH for my console, but still cannot get the gem to load in my console.
It may be that the gmail gem is conflicting with another gem that I have installed, but I don't know how to confirm this. It seems other people have had similar problems:
Why is autoload failing to load files for gems
Ruby autoload conflicts between gmail and parse_resource gems
Rails: Using ruby-gmail gem causes problems
I made sure to include 'gem "gmail"' in my Gemfile and run bundle install. Still no luck!
Stuck here, so would appreciate any help.
false doesn't mean that the gem is failing to load, it means that the gem is already loaded.
It the rails console could not find the gem you would get a LoadError. Here's an example of an app of mine that has gem 'haml' in the Gemfile.
1.9.2p320 :001 > require 'haml'
=> false
1.9.2p320 :002 > require 'foo'
LoadError: no such file to load -- foo
Another way to see this is to require 'gmail' a second time in your irb session.
You must add this line to your Gemfile:
gem 'gmail'
This is because the gems loaded by your application are restricted to just the ones specified inside the Gemfile, and their dependencies (and the dependencies of the dependencies, and so on).