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?
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?
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>
As far as I know localStorage has nothing to do with Rails, it is pure Javascript/HTML5 feature.
You can use the following in you application js in order to read or write data from the local storage:
var foo = localStorage.getItem("bar");
localStorage.setItem("bar", foo);
As others have already said local storage is Javascript/Html feature/solution but if wanting to learn how to integrate that with rails Ryan Bates has a railscast at http://railscasts.com/episodes/248-offline-apps-part-2, though you might need to watch part 1 to fully understand it.