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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 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
		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
4f9f63a237d544bb350e0f2fb53a7f24?d=identicon&s=25 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)
end
This topic is locked and can not be replied to.