0

Setting global variables in my js file doesn't seem to work. How can I set rails session variables from JavaScript?

I'd like a solution that doesn't use jQuery.


Edit: The solution is to use an HTML form containing hidden fields.

1 Answer 1

2

Global variables only live for the duration of a page view. Once the page is rerendered, they are reset to what their default values are.

If you need to keep the values, you need to use cookies or local storage. Other option is to submit them to the server with Ajax and have the server remember them and set the values when the page is rendered.

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

2 Comments

The page is not rerendered. The user clicks a button (causing me to need to save a variable) and then submits a form. During form submission, I need to have access to that variable.
So store the variable in a form hidden field and submit it to the server to use.

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.