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

人走茶凉 提交于 2019-11-30 14:30:29

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);
Egryan

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!