# Difference between local and live server

**URL:** <https://www.ruby-forum.com/t/difference-between-local-and-live-server/113394>\
**Category:** Rails\
**Created:** [August 30, 2007, 5:56pm UTC](https://www.ruby-forum.com/t/difference-between-local-and-live-server/113394 "2007-08-30T17:56:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [August 30, 2007, 5:56pm UTC](https://www.ruby-forum.com/t/difference-between-local-and-live-server/113394/1 "2007-08-30T17:56:49Z")

</div>

Yello,

I’m setting my layout in a before\_filter.

Here is what I have in application.rb:  
before\_filter :find\_current\_site

layout $layout

def find\_current\_site

if blahblahblah  
$layout = “joe1”  
else  
$layout = “joe2”  
end

end

It’s working on my local mongrel server, but on my live server (also  
mongrel) I always end up with no layout, no matter how many times I  
refresh.

Does anyone know what is going on here?

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [August 30, 2007, 6:49pm UTC](https://www.ruby-forum.com/t/difference-between-local-and-live-server/113394/2 "2007-08-30T18:49:17Z")

</div>

I’m using Ubuntu, if that makes any difference at all. Anyone have a  
clue why this is happening?

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [August 30, 2007, 7:03pm UTC](https://www.ruby-forum.com/t/difference-between-local-and-live-server/113394/3 "2007-08-30T19:03:18Z")

</div>

Hi,

> refresh.
> 
> Does anyone know what is going on here?

yep… when you are in development mode, the classes (most of them  
anyhow) get reloaded at each request. This means the code is  
reinterpreted every time and that’s why: a) you don’t need to restart so  
it’s cool to try things quickly, and: b) it’s much slower than running  
in production mode.

When in production, the classes get interpreted when loaded, and they  
are not reinterpreted every time. That’s why you need to restart the  
server when you change the code. This means the class variables will  
keep their values between calls unless you modify them explicitely. So  
what’s going on is first time you reference your controller, variable  
$layout doesn’t have any value, so you get a nil there. Then no matter  
what you do, the nil is there between calls.

There are different approaches to this, the most similar to your  
solution being just to use a proc instead of a value for the layout. You  
can say layout :find\_current\_site and then that method must return a  
String with the layout name. That should do the trick.

Also, using global variables is generally speaking a bad idea if you  
want to make solid modular code, so I’d try to avoid them when possible.

regards,

javier ramirez

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [August 30, 2007, 7:42pm UTC](https://www.ruby-forum.com/t/difference-between-local-and-live-server/113394/4 "2007-08-30T19:42:36Z")

</div>

Use an instance variable. Global variables are a bad idea in general,  
and especially in rails there is no need for them. One of the reasons  
we have before\_filters is to obviate the need for global variables.

On Aug 30, 10:56 am, Joe P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [August 30, 2007, 7:43pm UTC](https://www.ruby-forum.com/t/difference-between-local-and-live-server/113394/5 "2007-08-30T19:43:37Z")

</div>

Also, if you’re using this for a layout, you can simply do

layout :find\_current\_site

and it will use the return value to determine the layout.

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [August 30, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/difference-between-local-and-live-server/113394/6 "2007-08-30T19:39:21Z")

</div>

Thanks a bunch, that did the trick.

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [August 30, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/difference-between-local-and-live-server/113394/7 "2007-08-30T19:51:54Z")

</div>

unknown wrote:

> Also, if you’re using this for a layout, you can simply do
> 
> layout :find\_current\_site
> 
> and it will use the return value to determine the layout.  
> That’s actually exactly what I’m doing 🙂
