1

I am working in an application that uses Rails. I need to be able to set a variable for each user called language. Currently I do this through a ruby session variable that I setup in the controller, but when a user closes their browser I lose their language variable and they have to set it again every time they login. I need to attach this data to the user somehow. I have to use Ruby and Javascript on the front end and I can only make requests for data via Ruby.

<script type="text/javascript">
var language=getCookie(“language”);
  //***HELP use ajax to set user language preference in ruby***

function getCookie(name) {
  var value = "; " + document.cookie;
  var parts = value.split("; " + name + "=");
  if (parts.length == 2) return parts.pop().split(";").shift();
}
</script>
 
<% use language variable here to request data in ruby %>
Joshua Hedges
  • 307
  • 4
  • 16
  • set an expiration date on the cookie when you set it? – epascarello Aug 09 '17 at 18:54
  • My cookies have a high expiration. I don't show that code. My issue is how do I get the data from the user's browser into the ruby backend and still have it tied to the user. I need to set a ruby variable with ajax and javascript. – Joshua Hedges Aug 09 '17 at 20:58

1 Answers1

0

In ruby, by default, cookies only last till the session is closed. You can override that by just giving them an expiration date. Which means they'll last even if the user closes the browser, until they reach that expiration date, Just set it arbitrarily high and you'll be good to go.

TheCog19
  • 1,129
  • 12
  • 26