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

Chim Kan picture Chim Kan · Oct 9, 2012 · Viewed 15.1k times · Source

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?

Answer

Steffan Perry picture Steffan Perry · Oct 9, 2012

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>