1

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.

2
  • have you figured out yet? Commented Dec 1, 2015 at 17:35
  • 1
    There doesn't seem to be an explicit way to do this. Hence I believe that managing your variable through the database is the most sensible solution. Commented Dec 2, 2015 at 18:56

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0
$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.

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.