2

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?

Assaf Shomer
  • 1,429
  • 1
  • 15
  • 21

4 Answers4

4

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
3

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

Lil E
  • 388
  • 5
  • 23
1

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
lgx
  • 590
  • 4
  • 9
0

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.

Community
  • 1
  • 1
Rob Wise
  • 4,930
  • 3
  • 26
  • 31