Amr Abdelraouf
Amr Abdelraouf

Reputation: 13

Dynamically set a global variable in rails

I was wondering how to initialize and set a global variable in rails. For example if I was building a pizza delivery system and I want the admin to be able to "close" and "open" the place whenever he pleases.

Upvotes: 1

Views: 1066

Answers (3)

mmhan
mmhan

Reputation: 731

In addiction to @smathy's answer, if you wanted to avoid using database, simply File.open a index.html in your /public folder, but, of course, you won't be able to use any dynamically generated content on that page.

Upvotes: -1

dax
dax

Reputation: 10997

$pizza_store = :open

That's all. It's global so it doesn't need to be in any sort of namespace, but I would rethink using global variables for any reason.

Start with code school or something similar to learn basic Ruby, then try a more complete tutorial (the Michael Hartl one is good) and learn 'the Rails way' - because if you want to do Rails, you have to do it their way, or you're going to quickly get frustrated.

Upvotes: 0

smathy
smathy

Reputation: 27961

Setting a global variable is simple, just set it $open = false but that won't help you much in a live application because your application will probably be running across multiple processes (each with its own memory, and therefore its own global variables).

The simplest place to start is to just store this state in your database, and check it on each request that comes in where it's relevant.

Upvotes: 3

Related Questions