1

I'm not sure how it works when I want to request the ip address and know what it is. Right now I'm in development and in the rails sandbox console I do:

user$ rails console --sandbox
Loading development environment in sandbox (Rails 4.2.3)
Any modifications you make will be rolled back on exit
irb(main):001:0> ip = request.ip

and it returns:

NameError: undefined local variable or method `request' for main:Object
    from (irb):1
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/commands/console.rb:110:in `start'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/commands/console.rb:9:in `start'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/commands.rb:17:in `<top (required)>'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `require'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `block in require'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `require'
    from /home/user/app/bin/rails:8:in `<top (required)>'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `block in load'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/commands/rails.rb:6:in `call'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/command_wrapper.rb:38:in `call'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:183:in `block in serve'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:156:in `fork'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:156:in `serve'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `<top (required)>'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /home/user/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'irb(main):002:0>

I have no clue what's going on here. I'm trying to get Geocoder to convert an anonymous users ip address into a street address by doing this in my application controller:

  def set_location
    ip = request.ip
    @user_location = Geocoder.search(ip)
  end

But it doesn't work because I'm guessing of having no ip. How do I request the users ip correctly?

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
John Huntington
  • 397
  • 4
  • 18

2 Answers2

0

In Rails console you don't have request object.

If you want to debug request and it's ip you should do it at controller or view levels. Also for simplify it you should consider to use Pry console in it's binding.pry method. This method you can call in controller actions:

def index
  binding.pry
  # some code here
end

Or use it in views:

<% binding.pry %>
Alexander Shlenchack
  • 3,779
  • 6
  • 32
  • 46
0

You don't have request object here.

Inside the rails application:

request.remote_ip

Reference link

Community
  • 1
  • 1
praaveen V R
  • 1,259
  • 1
  • 11
  • 20