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?
-
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.Gabe– Gabe2011-08-10 04:06:31 +00:00Commented 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.rookieRailer– rookieRailer2011-08-10 04:12:53 +00:00Commented Aug 10, 2011 at 4:12
Add a comment
|
5 Answers
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.
1 Comment
Laurent
In rails the session is a cookie with a unique ID hash.
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
rookieRailer
I guess I wanted to say cookie variable, rather than session variable.
rookieRailer
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.
run
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
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
you can render js view in rails and put there session variable. for mo info see http://railscasts.com/episodes/205-unobtrusive-javascript