pry gem how to reload?

不打扰是莪最后的温柔 提交于 2019-11-27 20:39:08

问题


I am using the Pry gem in my Rails console, but the pry flavored rails-console seems to have lost the reload! method for reloading models and stuff.

Here's how I start the pry console

c:\rails\app> pry -r ./config/environment

Thank You


回答1:


You could check out this page on the Pry wiki: https://github.com/pry/pry/wiki/Setting-up-Rails-or-Heroku-to-use-Pry

Also check out the pry-rails plugin: https://github.com/rweng/pry-rails

There's also a lot of other content on that wiki, it's a great resource.




回答2:


To use reload! like the rails console command, add this code to your .pryrc

# load Rails Console helpers like reload
require 'rails/console/app'
extend Rails::ConsoleMethods
puts 'Rails Console Helpers loaded'

EDIT== Gem pry-rails already do all of this, much simplier .




回答3:


For anyone coming to this question recently: the answer has changed in Rails 3.2, because they've changed how they implement reload! Where in earlier version the irb commands were added as methods to Object, now they are added to IRB::ExtendCommandBundle to avoid polluting the global namespace.

What I do now is (1) in development.rb

silence_warnings do
  begin
    require 'pry'
    IRB = Pry
    module Pry::RailsCommands ;end
    IRB::ExtendCommandBundle = Pry::RailsCommands
  rescue LoadError
  end
end

and (2) in .pryrc

if Kernel.const_defined?("Rails") then
  require File.join(Rails.root,"config","environment")
  require 'rails/console/app'
  require 'rails/console/helpers'
  Pry::RailsCommands.instance_methods.each do |name| 
    Pry::Commands.command name.to_s do 
      Class.new.extend(Pry::RailsCommands).send(name)
    end
  end
end

Here's the link to the Rails pull request where the change was introduced - https://github.com/rails/rails/pull/3509




回答4:


You could tell Pry to load your Rails environment in your .pryrc

rails = File.join Dir.getwd, 'config', 'environment.rb'

if File.exist?(rails) && ENV['SKIP_RAILS'].nil?
  require rails

  if Rails.version[0..0] == "2"
    require 'console_app'
    require 'console_with_helpers'
  elsif Rails.version[0..0] == "3"
    require 'rails/console/app'
    require 'rails/console/helpers'
  else
    warn "[WARN] cannot load Rails console commands (Not on Rails2 or Rails3?)"
  end
end

This will give your reload! back.




回答5:


I've recently written a post about pry and rails. You can find it here http://lucapette.com/pry/pry-everywhere/. By the way, as dave already said, you would like to use pry with:

pry -r ./config/environment

I recommend you to try what I wrote in the article, it works really fine.




回答6:


alias pryr="pry -r ./config/environment -r rails/console/app -r rails/console/helpers"



回答7:


If you are having trouble with Zeus and Pry, try adding to your .pryrc:

if Kernel.const_defined?(:Rails) && Rails.env
  require File.join(Rails.root,"config","environment")
  require 'rails/console/app'
  require 'rails/console/helpers'
  extend Rails::ConsoleMethods
end

Taken from here




回答8:


Do you mean ./config/environment?

In any case, I think that's different than actually launching a rails console, which is where reload! comes from. I redefine IRB = Pry in my env-specific config file, which ensures a full console, and it all works like a charm.




回答9:


A better version of @Rodrigo Dias's answer. If you don't want to use pry-rails gem then just add following into your .pryrc-

if defined?(Rails) && Rails.env
  if defined?(Rails::ConsoleMethods)
    include Rails::ConsoleMethods
  else
    def reload!(print=true)
      puts "Reloading..." if print
      ActionDispatch::Reloader.cleanup!
      ActionDispatch::Reloader.prepare!
      true
    end
  end
end

This code properly identifies environments and doesn't blindly includes Rails::ConsoleMethods.

Source - Github thread comment



来源:https://stackoverflow.com/questions/7347327/pry-gem-how-to-reload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!