Auto include in Rails Console

2019-05-21 10:46发布

I find myself having to type (for example)

include PathHelper

every time I load the Rails Console.

Is there a way to configure the Rails console to automatically include certain modules?

4条回答
我欲成王,谁敢阻挡
2楼-- · 2019-05-21 11:26

Just in case anyone still feel confused, the simplest way to do this is:

  1. go to the root directory of your project
  2. create an .irbrc file(if you use rails console) or .pryrc file(if you use pry)
  3. put whatever you need to include in it

For example, if you use the default rails console and need to include PathHelper, just put it in the file:

# RootDirectoryOfYourProject/.irbrc 
include PathHelper

The PathHelper will be included automatically when you do rails console

查看更多
虎瘦雄心在
3楼-- · 2019-05-21 11:35

I would check out this question.

Basically, modify your config/application.rb file to include the paths to any modules you want to auto-load.

查看更多
做自己的国王
4楼-- · 2019-05-21 11:38

If you are still looking for an answer, this is what I do, I created a file ~/.irbrc in which you put all the code you want to be auto loaded in your rails console.

This is the content of my file:

require "awesome_print"
include Rails.application.routes.url_helpers

AwesomePrint.irb!
def y(obj)
  puts obj.to_yaml
end
查看更多
ら.Afraid
5楼-- · 2019-05-21 11:46

The syntax for configuring rails console has changed. I found this on 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
查看更多
登录 后发表回答