I'm trying to save a variable called persistent_data
.
I usually use session[:persistent_data]
or cookies[:persistent_data]
, but I would like to use the localstorage instead.
How do I do that on Rails?
The localStorage can store only strings. To store objects, you convert them to strings using the JSON. stringify() method. And you convert the strings into objects when you retrieve them from the localStorage using the JSON.
Storage setItem() Method The setItem() method sets the value of the specified Storage Object item. The setItem() method belongs to the Storage Object, which can be either a localStorage object or a sessionStorage object.
Many browser extensions store their data in the browser's so-called Local Storage, which is nothing else than a storage location managed by the web browser. And as the same suggests, all is saved locally on the machine where the browser is installed. Local storage is not in the cloud.
Localstorage has nothing to do with rails. You do it the same way as with any other language:
<script>
localStorage.setItem("company_id", "1");
</script>
localStorage.getItem("company_id");
=> 1
You can use rails to dynamically set the item however:
<script>
localStorage.setItem("company_id", "<%= @company.id %>");
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With