Forum: Ruby on Rails @request et al not available in controller initialize?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
cool_screen_name90001 (Guest)
on 2005-11-12 12:49
(Received via mailing list)
Is @request and the other controller attributes not
available in a controller's initialize function? What
I'm trying to do:

	def initialize

		server_name = @request.env["SERVER_NAME"]

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

It gives this error:

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


Yahoo! Mail - PC Magazine Editors' Choice 2005
lgoats (Guest)
on 2005-11-12 12:49
(Received via mailing list)
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)
This topic is locked and can not be replied to.