Cookies vs Sessions with CookieStore

前端 未结 2 1943
时光说笑
时光说笑 2021-01-29 22:51

In Rails 3, what is the difference between storing data in a cookie and storing data in a session, with the session store set to the default of CookieStore?

e.g.

2条回答
  •  我在风中等你
    2021-01-29 23:17

    The main difference is that when you use cookie[:foo] = 'bar' the user is able to see the value for the cookie, i.e. 'bar'. When you use session[:foo] = 'bar' the value will be encrypted by rails and stored in the _myapp_session cookie.

    You would use the cookie[] format when the information you want to store is not bound to the session, e.g. when the users selects the preferred language.

    You would use the session[] format when you want to store information that is related to the current session, e.g. the id of the the user.

提交回复
热议问题