自动包含在Rails控制台中

我发现自己必须输入(例如)

包括PathHelper

每次我加载Rails控制台。

有没有办法配置Rails控制台自动包含某些模块?

如果您仍在寻找答案,这就是我的工作,我创建了一个文件~/.irbrc ,您可以将所有想要自动加载的代码放在rails控制台中。

这是我的文件的内容:

 require "awesome_print" include Rails.application.routes.url_helpers AwesomePrint.irb! def y(obj) puts obj.to_yaml end 

配置rails控制台的语法已更改。 我在RailsGuides上找到了这个:

http://guides.rubyonrails.org/configuring.html#rails-general-configuration

 console do # this block is called only when running console, # so we can safely require pry here require "pry" config.console = Pry end 

万一有人仍然感到困惑,最简单的方法是:

  1. 转到项目的根目录
  2. 创建.irbrc文件(如果使用rails console )或.pryrc文件(如果使用.pryrc
  3. 把你需要的东西包括在里面

例如,如果您使用默认的rails控制台并且需要include PathHelper ,只需将其放在文件中:

 # RootDirectoryOfYourProject/.irbrc include PathHelper 

当您执行rails console时,将自动包含PathHelper

我会查看这个问题 。

基本上,修改config/application.rb文件以包含要自动加载的任何模块的路径。