Session values are available on the server.
You can install them as in your controller :
session[:user_name] = @user.name
If you want to access this value later in javascript, you probably want to do something like this in the view:
<%= javascript_tag do %> var userName = '<%= session[:user_name %>'; <% end %>
Cookies are managed by the browser, so access is different.
To install it in your controller:
cookies[:user_name] = @user.name
(You can also specify the path, expiration, etc. for the cookie using the parameters .)
Then it can be accessed using jQuery:
var userName = jQuery.cookie("user_name");
Note: you can also access the cookie using pure javascript (not jQuery) by parsing document.cookie , but it is much easier to let jQuery do it for you (if you already use this library).
source share