Luís Ramalho
Luís Ramalho

Reputation: 10218

NameError: undefined local variable or method `app' for main:Object

I would like to know if when using pry is possible to have access to the variable app?

As an example, when I try to access the root_path I get the following error:

[14] pry(main)> app.root_path
NameError: undefined local variable or method `app' for main:Object

Someone said that "It does now work with pry and 3.2.9". I am using rails 3.2.12, but it doesn't seem to work.

I have gem 'pry' in my GemFile group development and in config/environments/development.rb the following

  # Use Pry instead of IRB
  silence_warnings do
    begin
      require 'pry'
      IRB = Pry
    rescue LoadError
    end
  end

Upvotes: 2

Views: 2253

Answers (2)

VictorO
VictorO

Reputation: 147

Though this is solved, if you don't want to or can't use the 'pry-rails' gem for some reason you can also add the following into your .pryrc file:

if defined?(Rails) && Rails.env
  extend Rails::ConsoleMethods
end

You can read more in the pry wiki

Upvotes: 0

deefour
deefour

Reputation: 35370

Yes, it works

➜  MyApp git:(master) rc
Loading development environment (Rails 3.2.13)
[1] pry(main)> app.root_path
=> "/"

I use pry-rails in favor of your overriding of IRB in an initializer.

group :development do
  gem 'pry-rails'
end

https://github.com/rweng/pry-rails

Upvotes: 1

Related Questions