How do I save data on LocalStorage in Ruby on Rail

2019-02-10 16:10发布

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?

3条回答
三岁会撩人
2楼-- · 2019-02-10 16:43

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);
查看更多
干净又极端
3楼-- · 2019-02-10 16:51

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>
查看更多
你好瞎i
4楼-- · 2019-02-10 16:56

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.

查看更多
登录 后发表回答