Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save data on LocalStorage in Ruby on Rails 3.2.8?

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?

like image 327
Chim Kan Avatar asked Oct 09 '12 18:10

Chim Kan


People also ask

How do I save a string in localStorage?

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.

What method is used to store a value in local or session storage?

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.

Where is local storage saved?

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.


1 Answers

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>
like image 152
Steffan Perry Avatar answered Oct 17 '22 06:10

Steffan Perry