# @request et al not available in controller initialize?

**URL:** <https://www.ruby-forum.com/t/request-et-al-not-available-in-controller-initialize/93889>\
**Category:** Rails\
**Created:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/request-et-al-not-available-in-controller-initialize/93889 "2005-11-12T12:49:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/request-et-al-not-available-in-controller-initialize/93889/1 "2005-11-12T12:49:41Z")

</div>

Is @request and the other controller attributes not  
available in a controller’s initialize function? What  
I’m trying to do:

```
def initialize
	super

	server_name = @request.env["SERVER_NAME"]

	@site=Site.find :first,
		:conditions => ["server_name=?", server_name]
end

```

It gives this error:

You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.env

thanks  
csn

* * *

Yahoo! Mail - PC Magazine Editors’ Choice 2005

> **[Yahoo Mail - Organized Email](https://mail.yahoo.com)**
>
> It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/request-et-al-not-available-in-controller-initialize/93889/2 "2005-11-12T12:49:47Z")

</div>

You should probably use a before\_filter for this…

before\_filter do |controller|  
server\_name = controller.request.env[“SERVER\_NAME”]  
@site = Site.find\_by\_server\_name(server\_name)  
end
