@request et al not available in controller initialize?


#1

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
http://mail.yahoo.com


#2

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