2011-09-08 57 views
30

我在我的Rails控制檯中使用Pry gem,但pry flavored rails-console似乎已經丟失了重新加載!重新加載模型和東西的方法。pry寶石如何重新加載?

以下是我開始撬控制檯

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

謝謝

+0

由於兩者

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 

,大家好! –

回答

1

你的意思./config/environment

在任何情況下,我認爲這與實際啓動導軌控制檯不同,後者是reload!的來源。我在我的env特定的配置文件中重新定義了IRB = Pry,這確保了完整的控制檯,並且它的所有功能都像魅力一樣。

2

我最近寫了一篇關於pry和rails的文章。你可以在這裏找到它http://lucapette.com/pry/pry-everywhere/。順便說一句,戴維已經說了,你想用撬帶:

pry -r ./config/environment 

,我建議你去嘗試什麼,我在文章中寫到,它的作品真的很好。

+0

您可以使用'RAILS_ENV = production pry -r。/ config/environment'在生產環境中使用。 –

6

你可以告訴撬起以載入Rails環境的.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 

這將給你reload!回來。

+0

感謝Netmute,這從目前爲止我能看到的效果最好。 –

+1

ahhhh,重新加載!似乎正在工作,但實際上沒有重新加載任何模型;然而,從班尼斯特的答案插件似乎做到了這一點 –

+2

在Rails 3.2我還需要說'include Rails :: ConsoleMethods' – Peter

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

對於沒有人來對這個問題最近:答案已在Rails的3.2改變了,因爲他們已經改變了他們如何實現reload!凡在早期版本的IRB命令被添加方法Object,現在他們都加入到IRB::ExtendCommandBundle以避免污染全局名稱空間。

我現在要做的是:(1)development.rb

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

和(2)在.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 

這裏的鏈接到Rails的拉請求,其中引入的變化 - https://github.com/rails/rails/pull/3509

+2

pry-rails與rails的合作3.2 – tee

15

要使用重新加載!像鐵軌控制檯命令,將此代碼添加到您的.pryrc

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

編輯== 寶石撬滑軌已經做到這一切的,很多simplier的。

+0

與gem 「撬」當然安裝 –

2

如果您有宙斯和撬麻煩,儘量增加你的.pryrc:從here