ApplicationController and request.host

Perhaps I’m not understanding this very well. I want to set a layout
based on a domain name.

The following works in ApplicationController.rb:

before_filter :set_host
case @host
when ‘site1.com
layout ‘site1’
when ‘site2.com
layout ‘site2’
else
layout ‘default’
end
def set_host
@host = request.host
end

This returns an error for an undefined “request” method:

case request.host
when ‘site1.com
layout ‘site1’
when ‘site2.com
layout ‘site2’
else
layout ‘default’
end

Why doesn’t the second one work?

Thanks,
Bill

Because the actual controller doesn’t know about the request object
until
you call a method inside of it, like you’re doing with set_host.
Put your case statement inside a method in any controller and it will
work
with request.host.

On Dec 28, 2007 4:34 PM, Bill D. [email protected]
wrote:

when ‘site2.com
case request.host
Thanks,
Bill

Posted via http://www.ruby-forum.com/.


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

Ryan B. wrote:

Because the actual controller doesn’t know about the request object
until
you call a method inside of it, like you’re doing with set_host.
Put your case statement inside a method in any controller and it will
work
with request.host.

Thanks. I understand what you’ve said but why is controller only aware
of the request object inside a method?

Bill

Bill D. wrote:

In addition, this doesn’t work:

before_filter :determine_layout
def determine_layout
case request.host
when ‘site1.com
layout ‘site1’
when ‘site2.com
layout ‘site2’
else
layout ‘application’
end
end

It reports there is an undefined method ‘layout’. Why does the layout
method disappear in the method?

It turns out none of this is working. Anyone know why the request
object is not available in ApplicationController in Rails 2.01? Now,
off to check 2.02…

In addition, this doesn’t work:

before_filter :determine_layout
def determine_layout
case request.host
when ‘site1.com
layout ‘site1’
when ‘site2.com
layout ‘site2’
else
layout ‘application’
end
end

It reports there is an undefined method ‘layout’. Why does the layout
method disappear in the method?

This works:

layout :determine_layout
def determine_layout
case request.host
when ‘site1.com’ : ‘site1’
when ‘site2.com’ : ‘site2’
else ‘application’
end
end

Now I can make a multi-domain site.

Bill