[英]How do I load a ruby variable via Ajax and Javascript?
I am working in an application that uses Rails. 我正在使用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.
目前,我是通过控制器中设置的ruby session变量来完成此操作的,但是当用户关闭浏览器时,我会丢失语言变量,因此每次登录时都必须重新设置该变量。 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.
我必须在前端使用Ruby和Javascript,并且只能通过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 %>
In ruby, by default, cookies only last till the session is closed. 在ruby中,默认情况下,cookie仅持续到会话关闭。 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.
这意味着即使用户关闭浏览器它们也将持续使用,直到达到该到期日期为止,只要将其设置为任意高就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.