# Request.env not accessible in controller

**URL:** <https://www.ruby-forum.com/t/request-env-not-accessible-in-controller/106967>\
**Category:** Rails\
**Created:** [June 15, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/request-env-not-accessible-in-controller/106967 "2007-06-15T21:33:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabordemeter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9de053/32.png) [@gabordemeter](https://www.ruby-forum.com/u/gabordemeter)\
**Post date:** [June 15, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/request-env-not-accessible-in-controller/106967/1 "2007-06-15T21:33:00Z")

</div>

Hi,

I have the simplest rails app:

my only controller says:

class MaxmindController \< ApplicationController  
@ip = request  
end

my index.rhtml in the views/maxmind folder says:

\<%= debug @ip %\>

the result of trying to view this in firefox is:

NameError in MaxmindController#index

undefined local variable or method `request’ for  
MaxmindController:Class

if I remove the one line from my controller and change my view code  
to:

\<%= debug request %\>

then i get my request object nicely displayed.

Why is request available only in the view and not in the controller?  
I’m running this locally under locomotive.

Thanks,

Gabor

---

<div class="post-metadata">

**Author:** ![gabordemeter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9de053/32.png) [@gabordemeter](https://www.ruby-forum.com/u/gabordemeter)\
**Post date:** [June 15, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/request-env-not-accessible-in-controller/106967/2 "2007-06-15T21:41:02Z")

</div>

Because you’re out of scope. @ip = request is in the class scope, and  
request is not available there.

Try using a before\_filter.

class MaxmindController \< ApplicationController  
before\_filter :get\_ip

protected  
def get\_ip  
@ip = request.remote\_ip  
end

end
