Difference between local and live server

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?

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

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

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. [email protected]

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.

Thanks a bunch, that did the trick.

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 :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs