6

I need to render an HTML element in rails, depending upon whether a session variable is set or not. Is it possible to do something like this?

2
  • Session is server side, I am not a rails developer, but in asp.net mvc I made an ajax call that gets the Session value and returns it back to client. Just an idea. Commented Aug 10, 2011 at 4:06
  • That seems logical. Actually, I am trying to implement a feature, mentioned here, and thought that I could save the state using session variables, and then read those variables at the time of page load and then do the needful. Could you check out that post? Thanks. Commented Aug 10, 2011 at 4:12

5 Answers 5

5

Session is server side, I am not a rails developer, but in asp.net mvc I made an ajax call that gets the Session value and returns it back to client. Just an idea.

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

1 Comment

In rails the session is a cookie with a unique ID hash.
1

You cannot access any server side variable from client side. You can basically render the session variable value in a javascript variable and use it on the client side in jquery.

3 Comments

I guess I wanted to say cookie variable, rather than session variable.
Actually, I am trying to implement this feature, and thought that I could save the state using session variables, and then read those variables at the time of page load and then do the needful. Could you check out that post? Thanks.
http://plugins.jquery.com/project/Cookie go through docs and you will easily get to know the rest with setting getting and expiring these cookies
0

A small library that helps you read write cookies.

var cookieHelper = (function () {
    return {
        createCookie: function (name, value, days) {
            var expires = "";
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                expires = "; expires=" + date.toGMTString();
            }
            document.cookie = name + "=" + value + expires + "; path=/";
        },
        writeSessionCookie: function (cookieName, cookieValue) {
            document.cookie = cookieName + "=" + cookieValue + "; path=/";
        },
        readCookie: function (name) {
            var nameEq = name + "=";
            var ca = document.cookie.split(';');
            var i;

            for (i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) === ' ') { c = c.substring(1, c.length); }
                if (c.indexOf(nameEq) === 0) { return c.substring(nameEq.length, c.length); }
            }

            return null;
        },
        deleteCookie: function (name) {
            this.createCookie(name, null, -1);
        },
        testSessionCookieEnabled : function() {
           document.cookie ="testSessionCookie=Enabled";
            if (getCookieValue ("testSessionCookie")=="Enabled")
              return true 
            else
              return false;
        }
    };
 }());

Usage:

if(cookieHelper.testSessionCookieEnabled)
{
  cookieHelper.writeSessionCookie("test","session");
}
else
{
  //do something
}

var cookieValue=cookieHelper.readCookie("test");

Comments

0

you can render js view in rails and put there session variable. for mo info see http://railscasts.com/episodes/205-unobtrusive-javascript

Comments

0

If you are using rails 3 and storing your session variables using cookie store, then you should be able to access the session variables using JS just like accessing a typical cookie.

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.